Jump to content

CSV import to mysql


max101

Recommended Posts

Hi All,

 

1st time here, and first question. I have been looking at a load of csv scripts, they all seem to work but I need them to not read the 1st row as this is the table labels **INV**NAME**  3  **Dave**  in my csv its looks like this INV, NAME, 3, Dave but till now the whole csv file gets put in.

Also I have read there are memory issues as well as I am trying to upload like 4000 to 6000 entries, will this be an issue?

Here is the script I have, if anyone can help it would be great thanks in advance.

 

<?php

$connection = mysql_connect("localhost", "username", "pwd") or die ("Unable to connect to server");
       $db = mysql_select_db("csv_test", $connection) or die ("Unable to select database");

$fcontents = file ('./test.csv');

# expects the csv file to be in the same dir as this script

for($i=0; $i<sizeof($fcontents); $i++) {

         $line = trim($fcontents[$i],',');   //   '\t' for tab delimeted
        $arr = explode(',', $line);         //   '\t' for tab delimeted

        #  if your data is comma separated
        #  instead of tab separated
        #  change the '\t' above to ','

        $sql = "insert into user values ('". implode("','", $arr)
."')";

        $sql = str_replace("''", mysql_escape_string("NULL"), $sql);

        $sql = str_replace("' '", mysql_escape_string("NULL"), $sql);

        mysql_query($sql); echo $sql ."<br>\n";

        if(mysql_error()) {
                echo mysql_error() ."<br><b><font color=red>\n</b>";
        }

} ?>

Link to comment
https://forums.phpfreaks.com/topic/191764-csv-import-to-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.