
August 31st, 2006, 11:35 PM
|
|
|
|
Join Date: Apr 2007
Posts: 10
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Importing CSV into DB
Ok, so I have spent a lot of time here today trying to get the update right. The issue that I am having is that the following code just grabs one value from the CSV file:
php Code:
Original
- php Code |
|
|
|
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $row++; for ($counter=$columnheadings; $counter < $num; $counter++) { echo $data[$counter] . "<br />n"; $insertrecord = "Insert Into `$tablename` Values ($data[$counter])";
So I changed things around to format a SQL statement that will run. The problem is that I cannot get the string replace function to remove the last comma. I know the fix is probably simple, but I guess I am tired and out of ideas. Or maybe there is a better way to do this. In any case, I appreciate the help. Here's the current code:
php Code:
Original
- php Code |
|
|
|
$handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "<p> $num fields in line $row: <br /></p>n"; $insertrecord = "Insert Into `$tablename` Values ("; $row++; for ($c=0; $c < $num; $c++) { $insertrecord .= "'$data[$c]',"; } $insertrecord .= ")";
|