Jump to content

Loop Not Working


shage

Recommended Posts

$handle = fopen("csv.txt", "r");
while ($line = fgetcsv($handle, 1000, ",")) {
$title = $line[0];
$url = $line[1];
$image = $line[2];
$query = "INSERT INTO videos (`title`,`url`, `image`, `date`, `spon`, `cat`) VALUES ('$title','$url', '$image', '2008-09-25', 'NA', 'NM')";
$result = mysql_query($query);
} 
fclose($handle);

 

Some reason it only adds the first line of the csv, thank you

Link to comment
https://forums.phpfreaks.com/topic/125884-loop-not-working/
Share on other sites

You are probably having a line-ending problem and the whole file is being read into the first line. Display the actual data to see what is being read from the file. Add the following inside of your loop -

 

echo "<pre>";
print_r($line);
echo "</pre>";

Link to comment
https://forums.phpfreaks.com/topic/125884-loop-not-working/#findComment-650937
Share on other sites

I'm going to guess that your table defines title as an index and the second query is failing due to a duplicate index error.

 

All code must check for any possible error, how else will you know if it worked or not? At a minimum, change your mysql_query to get mysql/php to tell you if the query fails and why it failed -

 

$result = mysql_query($query) or die("Query failed: $query<br />Mysql error: " . mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/125884-loop-not-working/#findComment-650947
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.