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
    • 551
    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, indresh@abc.com, 12345
    (2) mukul kant, mukul.kant@abc.com, 12345

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

     <?php
            //Database connection
            $db = mysql_connect("localhost", "root", "123456");
            if(!$db){
                echo 'Mysql Connection Error';
            }
            if(!mysql_select_db("import",$db)){
    
                echo 'Database Not Present'.mysql_error();
            }
            $deleterecords = "TRUNCATE TABLE users"; 
            //use for empty the table of its current records
            mysql_query($deleterecords);
    
            //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]."')";
    
                    mysql_query($importQuery);
                }
    
                fclose($handle);    
                echo 'File Impot Successfully';  
                //view upload form
            }else { 
    
            #Html file tag includes#
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: