LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2010, 05:18 PM   #1
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Rep: Reputation: 0
php undefined offset


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($handle1000",");
$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($handle1000",");
$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
                )"
;

$sqlData mysql_query($sql); 
can anyone please assist me?

Last edited by eoc92866; 12-08-2010 at 06:22 PM.
 
Old 12-09-2010, 11:31 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
You could do like that:

$data = fgetcsv($handle, 1000, ",");
foreach ($data as &$d) {
if (empty($d)) {
$d = 'default value';
}
}
Then INSERT...
 
Old 12-09-2010, 02:42 PM   #3
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 333

Rep: Reputation: 56
Quote:
Originally Posted by eoc92866 View Post
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($handle1000",");
$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($handle1000",")) {
    
$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'";

         if (
$i count($data) - 1) {
             
$sql .= ", ";
         }
    }

    
$sql .= ");";
}

$sqlData mysql_query($sql); 
[untested, may not work exactly as is]
 
Old 12-09-2010, 05:15 PM   #4
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
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?
 
Old 12-10-2010, 10:22 AM   #5
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 333

Rep: Reputation: 56
Quote:
Originally Posted by eoc92866 View Post
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....

something like:
PHP Code:
$sql .= "values ('" implode("', '"$data) . "')"
for the values, and a combination of array_keys() and implode() for the field names:
PHP Code:
$sql .= "(name" implode(", name"array_keys($data)) . ")"
[again, untested, may not work exactly as is]
 
Old 12-10-2010, 03:19 PM   #6
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
As far as I understand that you have a table with 46 columns and want to read in a csv file completely?

// foreach ($data as $i => $item) { $i is 0 at the start
// $sql .= "name$i"; so the column names are not matched ??

while ($data = fgetcsv($handle, 1000, ",")) {

//kick out empty
foreach ($data as &$d) {
if (empty($d)) {
$d = 'default value';
}
}
//create sql
$sql = "insert into tableName (";
for ($i = 1; $i <47 ; $i++) {
$sql .= "name$i";

if ($i < 46) {
$sql .= ", ";
}
}
$sql .= " ) ";
$sql .= "values ('" . implode("', '", $data) . "')";

$sqlData = mysql_query($sql);

}

Last edited by j-ray; 12-10-2010 at 03:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Notice: Uninitialized string offset: 1 in [PHP] Aquarius_Girl Programming 8 11-10-2010 10:09 PM
PHP Problem ('undefined index', 'undefined function') zokken Programming 2 12-04-2008 10:18 AM
Urgent PHP problem with "undefined function: domxml_open_mem()"! Recomplie php? Oskare100 Linux - Server 0 12-27-2006 12:28 PM
undefined function scandir() in php tooparam Linux - Newbie 1 01-22-2006 01:03 AM
PHP Undefined index Error Gerardoj Programming 1 05-30-2004 03:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration