Click to See Complete Forum and Search --> : grabbing data from csv


Genixdeae
04-14-2005, 10:22 AM
I have a script that uploads a csv file to a server then opens it to grab the data then insert it into the database. But the script keeps timing out. What im having the code do might not even work even if it didnt time out but it seems logical. any help appriciated.

BTW here's the code: $handle = fopen($_FILES["teacher_info"]["name"], "r");
while(($data = fgetcsv($handle, 1024, ",")) !== FALSE) {
$num = count($data);
$insert_teachers = "INSERT INTO `combo` (`firstname`, `lastname`, `fd_password`";
for($i=1;$i<=$num_per;$i++) {
$insert_teachers .= ", `period$i`";
}
$insert_teachers .= ") VALUES (";
for($d=0;$d<=$num;$i++) {
if($data[$d] == "") {
$tdata = "NULL";
}else{
$tdata = $data[$d];
}
$insert_teachers .= "'". $tdata ."',";
}//This is the line is times out on. It is the end of the for() statement
$insert_teachers .= ")";
}
fclose($handle);
echo $insert_teachers;

NogDog
04-14-2005, 11:11 AM
You increment the wrong variable in this loop expression:
for($d=0;$d<=$num;$i++)
You want to change $i++ to $d++. Also, since your array indexes start with 0, you want $d<$num, not <=. So...
for($d=0; $d<$num; $d++)

Genixdeae
04-14-2005, 11:16 AM
heh, oops :P

Thanks for the help :P

NogDog
04-14-2005, 11:20 AM
No problem. I looked at that loop expression a couple times because I knew you wanted < instead of <=, but I didn't think that would cause a timeout error. Then as I was about to type a reply to that effect, it finally registered that you had the wrong variable in that increment expression. :)