csv to mysql help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eo92866
Forum Newbie
Posts: 2
Joined: Thu Dec 02, 2010 7:25 pm

csv to mysql help

Post by eo92866 »

the code below isn't populating the mysql fields... i believe it's not running correctly at the while and for loop areas. i've attempted to place echo at different places to get some kind of feedback with no luck.

also, would like to know of a way to have the 1000 to have the ability to look at the end of the row and then look at the next row of data, because there are sometimes +/-1000 characters in a row.

can someone please assist?
many thanks.

Code: Select all

<?php

//connect
 to the database
include "opendb.php";


#if the first row of csv file contains column headings:
$columnheadings = 1;

if (($handle = fopen("/my/file
/destination/name.csv", "r")) !== FALSE)
        {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                {
                $num = count($data);
#               echo "<p> $num fields in line $columnheadings: <br /></p>\n";
                $columnheadings++;
                        for ($i=0; $i<$num; $i++)
                                {
                                $line = trim($data[$i],'",');
                                $arr = explode(',',$line);

                                //mysql insert of values
                                $sqlData = ("INSERT INTO thisTable(name1, name2, name3, name4, name5) VALUES('".$arr[0]."', '".$arr[1]."', '".$arr[2]."', '".$arr[3]."', '".$arr[4]."')");

                        }
                }
        fclose($handle);
        }


else
        {
        mysql_query($sqlData) or
        die(mysql_error("The specified file does not exist. Thanks."));
        }

mysql_close($con);

?> 
Post Reply