ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi... i'm getting an undefined offset for the associative arrays [28-46] in this format.
PHP Code:
$row = 1;
$i = 0;
while ($i<10000) { $data = fgetcsv($handle, 1000, ","); $sql="INSERT INTO tableName (name1, name2, name3, name4... this goes all the way to name46) VALUES ('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."' ...this goes all the way to name46)";
$sqlData = mysql_query($sql);
i have read that i can prevent the notices by doing the following, but it's not working for me
PHP Code:
$row = 1;
$i = 0;
while ($i<10000) { $data = fgetcsv($handle, 1000, ","); $sql="INSERT INTO tableName ( 'name1' => "isset(.$data[0].) ? $data[0] : 'default value'", 'name2' => "isset(.$data[1].) ? $data[1] : 'default value'", 'name3' => "isset(.$data[2].) ? $data[2] : 'default value'", 'name4' => "isset(.$data[3].) ? $data[3] : 'default value'", #this goes all the way to name46 )";
hi... i'm getting an undefined offset for the associative arrays [28-46] in this format.
PHP Code:
$row = 1;
$i = 0;
while ($i<10000) { $data = fgetcsv($handle, 1000, ","); $sql="INSERT INTO tableName (name1, name2, name3, name4... this goes all the way to name46) VALUES ('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."' ...this goes all the way to name46)";
$sqlData = mysql_query($sql);
This code doesn't make a lot of sense.. why are you hardcoding your $data array indexes? You can do all that using a loop. You're also missing a closing curly brace. Is the call to mysql_query() inside the loop, or outside? Also, you're not incrementing $i at all, which means $i will ALWAYS be 0, and the loop will never terminate. Actually, that's probably your problem right there. You run out of data in the file, which means $data is null, and then you try to use it like an array. You can't can't check if $data[0] exists if $data itself doesn't exist.
Judging by what it appears you are trying to do, something like this might work better:
PHP Code:
while ($data = fgetcsv($handle, 1000, ",")) { $sql = "insert into tableName (";
foreach ($data as $i => $item) { $sql .= "name$i";
if ($i < count($data) - 1) { $sql .= ", "; } }
$sql .= ") values (";
foreach ($data as $i => $item) { $sql .= "'$item'";
i know this is a stupid question... but the name.$i... will i require to make an array for this area to place all of the different field names from the csv file into it?
i know this is a stupid question... but the name.$i... will i require to make an array for this area to place all of the different field names from the csv file into it?
No such thing as stupid questions, just stupid answers
That said though, I'm not completely sure that I understand your question. Are you asking if you'll need to create an array for the "name$i" line? If so, no, you do not. You are using the same $data array to create both parts of the query.
Actually, you can probably use implode() instead of a loop in both places....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.