Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP script to import csv data into mysql

Tags:

php

mysql

csv

I have tried the following code but getting some errors. Here I can read the input file but I am getting the following error:Deprecated: Function split() is deprecated in C:\wamp\www\aaj2\index.php on line 63. O/P: Found a total of 5124 records in this csv file.

<?php
$databasehost = "localhost"; 
$databasename = "test"; 
$databasetable = "sample"; 
$databaseusername="test"; 
$databasepassword = ""; 
$fieldseparator = ","; 
$lineseparator = "\n";
$csvfile = "filename.csv";
$addauto = 0;
$save = 1; 
$outputfile = "output.sql";        

        if(!file_exists($csvfile)) {    echo "File not found. Make sure you specified the correct path.\n";     exit; }

        $file = fopen($csvfile,"r");

        if(!$file) {    echo "Error opening data file.\n";  exit; }

        $size = filesize($csvfile);

        if(!$size) {    echo "File is empty.\n";    exit; }

        $csvcontent = fread($file,$size);

        fclose($file);

        $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error()); @mysql_select_db($databasename) or die(mysql_error());

        $lines = 0; $queries = ""; $linearray = array();

        foreach(split($lineseparator,$csvcontent) as $line) {

            $lines++;

            $line = trim($line," \t");      $line = str_replace("\r","",$line);         /************************************   This line escapes the special character. remove it if entries are already escaped in the csv file   ************************************/   $line = str_replace("'","\'",$line);    /*************************************/         $linearray = explode($fieldseparator,$line);        $linemysql = implode("','",$linearray);         if($addauto)        $query = "insert into $databasetable values('','$linemysql');";     else        $query = "insert into $databasetable values('$linemysql');";        $queries .= $query . "\n";

            @mysql_query($query); }

        @mysql_close($con);

        if($save) {         if(!is_writable($outputfile)) {         echo "File is not writable, check permissions.\n";  }       else {      $file2 = fopen($outputfile,"w");
                        if(!$file2) {           echo "Error writing to the output file.\n";         }       else {          fwrite($file2,$queries);            fclose($file2);         }   }    }

        echo "Found a total of $lines records in this csv file.\n";


        ?>

EDIT : Error : File is not writable, check permissions. Found a total of 5124 records in this csv file.

like image 805
Kabir Avatar asked Jan 02 '14 03:01

Kabir


People also ask

How do I import CSV data into MySQL?

In the Format list, select CSV. Changing format-specific options. If the csv file is delimited by a character other than a comma or if there are other specifications to the csv files, we can change it in this portion. Click Go to start importing the csv file and the data will be successfully imported into MySQL.

HOW include CSV file in PHP?

Access the CSV file utilizing PHP fopen() function. Parse data from the CSV record utilizing PHP fgetcsv() function. Insert or updade data into the database based on the member's e-mail.


2 Answers

Several tips:

  • Don't use the deprecated ext/mysql, when you can use ext/mysqli or PDO.

  • Don't read the entire csv file into a PHP variable. What happens when the file is 500MB?

  • Don't write custom PHP code to parse csv data, when you can use the builtin function fgetcsv().

  • Don't create a new SQL statement for every row in the data, when you can use prepared statements.

  • Don't interpolate data from an external file into SQL statements. This risks SQL injection vulnerabilities, just like when you interpolate untrusted user input.

  • Don't parse and insert csv data row by row, when you can use MySQL's LOAD DATA INFILE command. It's 20x faster than inserting row by row.

Here's a simpler solution:

<?php
$databasehost = "localhost"; 
$databasename = "test"; 
$databasetable = "sample"; 
$databaseusername="test"; 
$databasepassword = ""; 
$fieldseparator = ","; 
$lineseparator = "\n";
$csvfile = "filename.csv";

if(!file_exists($csvfile)) {
    die("File not found. Make sure you specified the correct path.");
}

try {
    $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", 
        $databaseusername, $databasepassword,
        array(
            PDO::MYSQL_ATTR_LOCAL_INFILE => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );
} catch (PDOException $e) {
    die("database connection failed: ".$e->getMessage());
}

$affectedRows = $pdo->exec("
    LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
      FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
      LINES TERMINATED BY ".$pdo->quote($lineseparator));

echo "Loaded a total of $affectedRows records from this csv file.\n";

?>

I tested this with PHP 5.3.26 on a Mac, connecting to MySQL 5.6.14 on Linux.

like image 186
Bill Karwin Avatar answered Oct 11 '22 16:10

Bill Karwin


Try this:

<?php
// specify connection info
$connect = mysql_connect('localhost','root','12345');
if (!$connect)
{
   die('Could not <span id="IL_AD1" class="IL_AD">
    connect to</span> MySQL: ' . mysql_error());
}

$cid =mysql_select_db('test',$connect); //specify db name

define('CSV_PATH','C:/wamp/www/csvfile/'); // specify CSV file path

$csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
   $csv_data[] = fgets($csvfile, 1024);
   $csv_array = explode(",", $csv_data[$i]);
   $insert_csv = array();
   $insert_csv['ID'] = $csv_array[0];
   $insert_csv['name'] = $csv_array[1];
   $insert_csv['email'] = $csv_array[2];
   $query = "INSERT INTO csvdata(ID,name,email)
     VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
   $n=mysql_query($query, $connect );
   $i++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysql_close($connect); // closing connection
?>
like image 33
ReNiSh AR Avatar answered Oct 11 '22 15:10

ReNiSh AR