Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to import csv file into mysql database in php

    • 0
    • 3
    • 3
    • 1
    • 0
    • 0
    • 0
    • 0
    • 526
    Comment on it

    To Import csv file into mysql database using php script follow the below code:

    For example: I have a .csv file data with this formate:-

    (1) Indresh Singh,, 12345
    (2) mukul kant,, 12345

    To insert this data into database, I am using the fallowing code below-

            //Database connection
            $db = mysql_connect("localhost", "root", "123456");
                echo 'Mysql Connection Error';
                echo 'Database Not Present'.mysql_error();
            $deleterecords = "TRUNCATE TABLE users"; 
            //use for empty the table of its current records
            //Upload File
            if (isset($_POST['submit'])) {
                //Import the uploaded file into Database
                $handle = fopen($_FILES['filename']['tmp_name'], "r");
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    $importQuery = "INSERT INTO users(name,email,password) VALUES ('".$data[0]."','".$data[1]."','".$data[2]."')";
                echo 'File Impot Successfully';  
                //view upload form
            }else { 
            #Html file tag includes#

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: