Page 1 of 1

"Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 2:23 am
by johnc71
I've tried everything and can't seem to get rid of the annoying "undefined offset" notices. The codedoes what it suppose to do but I would like ot know what I am doing wrong here. I searched for similar posts but can't seem to find the answer for my code.

Thanks in advance for your help!

Code: Select all

 
$sql1="SELECT * FROM mytable where active='yes'";
    $result1=mysql_query($sql1);
    while($rows1=mysql_fetch_array($result1)){
        $count='4';
        $id[]=$rows1['id'];
        for($i=0;$i<$count;$i++){
            $sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
            $result2=mysql_query($sql2);
            }
        }
 

Re: "Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 2:30 am
by onion2k
Undefined offset means you're trying to access an array offset that isn't set. In your code you loop through a result set adding an id to the $id array... but every time you add an id you also loop through the first 4 items of the $id array ... for the first 3 results of the result set there'll be fewer than 4 things in the $id array so when you access $id[$i] you get the undefined index error.

The solution is to loop through the number of items in $id, or 4 times, whichever is lower rather than always looping 4 times.

EDIT: Actually, reading the code again, what you're doing seems a bit odd. You're just updating all the records with 'active' set to "yes" to be "no" instead, right? I'd do "UPDATE `mytable` SET `active` = 'no' WHERE `active` = 'yes';".

Re: "Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 2:58 am
by johnc71
Thanks for your reply. What I am trying to do is, connect to db, update first 4 records where active is "yes" to "no".

I am planning to sell pin numbers so lets say I have DB with 100 records. Let's say customer buys 4 pins. I would like to go to DB, retrive first 4 available pins ("Active" = yes) and then set the "Active" field to "No" so that same pins will not be resold.

Hope this makes sense.

Btw, here is the exact code and error message.

Code: Select all

<?php
$host="myhost"; 
$username="myusername";
$password="mypassword";
$db_name="mydatabase";
 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
    $sql1="SELECT * FROM mytable where active='yes'";
    $result1=mysql_query($sql1);
    while($rows1=mysql_fetch_array($result1)){
        $count='4';
        $id[]=$rows1['id'];
        for($i=0;$i<$count;$i++){
            $sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
            $result2=mysql_query($sql2);
            }
        }
 
Here is the result:

Code: Select all

Notice: Undefined offset: 1 in /mycode.php on line 15
 
Notice: Undefined offset: 2 in /mycode.php on line 15
 
Notice: Undefined offset: 3 in /mycode.php on line 15
 
Notice: Undefined offset: 2 in /mycode.php on line 15
 
Notice: Undefined offset: 3 in /mycode.php on line 15
 
Notice: Undefined offset: 3 in /mycode.php on line 15
 

Re: "Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 3:15 am
by helraizer
johnc71 wrote:Thanks for your reply. What I am trying to do is, connect to db, update first 4 records where active is "yes" to "no".

I am planning to sell pin numbers so lets say I have DB with 100 records. Let's say customer buys 4 pins. I would like to go to DB, retrive first 4 available pins ("Active" = yes) and then set the "Active" field to "No" so that same pins will not be resold.

Hope this makes sense.

Btw, here is the exact code and error message.

Code: Select all

<?php
$host="myhost"; 
$username="myusername";
$password="mypassword";
$db_name="mydatabase";
 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
    $sql1="SELECT * FROM mytable where active='yes'";
    $result1=mysql_query($sql1);
    while($rows1=mysql_fetch_array($result1)){
        $count='4';
        $id[]=$rows1['id'];
        for($i=0;$i<$count;$i++){
            $sql2="UPDATE mytable SET active='no' WHERE id='$id[$i]'";
            $result2=mysql_query($sql2);
            }
        }
 
Here is the result:

Code: Select all

Notice: Undefined offset: 1 in /mycode.php on line 15
 
Notice: Undefined offset: 2 in /mycode.php on line 15
 
Notice: Undefined offset: 3 in /mycode.php on line 15
 
Notice: Undefined offset: 2 in /mycode.php on line 15
 
Notice: Undefined offset: 3 in /mycode.php on line 15
 
Notice: Undefined offset: 3 in /mycode.php on line 15
 
Not sure how much it'll help but on Line 13 you've got $id[]=$rows1['id'];

change that to

Code: Select all

 
$id[] .= $rows1['id'];
 
may help. Also $count = '4', '' being a string? That could be causing your error, even if it's not, if you have 5 elements in your array it won't find them. It'll find 0,1,2,3,4 so as Onion2k suggested, use count. So line 14 you'd use

Code: Select all

 
for($i=0;$i<Count($id);$i++) {
 
See if that helps.

Sam

Re: "Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 3:54 am
by onion2k
Retrieving 4 records and setting them to not active is pretty simple..

Code: Select all

<?php
$host="myhost";
$username="myusername";
$password="mypassword";
$db_name="mydatabase";
  
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
$arrIds = array();
 
$sql  = "SELECT `mytable`.* ";
$sql .= "FROM `mytable` ";
$sql .= "WHERE 1 ";
$sql .= "AND `mytable`.`active`= 'yes' ";
$sql .= "LIMIT 4"; //Limit the query to returning 4 records only.
 
if ($result=mysql_query($sql)) {
 
  if (mysql_num_rows($result)==4) {
 
    while($record=mysql_fetch_object($result)){
 
      $arrIds[] = $record->id;
 
    }
 
    $sql  = "UPDATE `mytable` SET ";
    $sql .= "`active` = 'no' ";
    $sql .= "WHERE 1 ";
    $sql .= "AND `id` IN (".implode(",",$arrIds).") "; //Implode takes an array and concatenates it into a string with a separator.
 
    mysql_query($sql);
 
  } else {
 
    echo "Fewer than 4 records returned";
 
  }
 
} else {
 
  echo "Query Failed";
 
}
 

Re: "Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 4:15 am
by johnc71
:bow: :bow: :D

Thanks. It worked great! I owe you a beer or whatever you are drinking :)

Can you please take a moment end explain what this code does?

while($record=mysql_fetch_object($result)){

$arrIds[] = $record->id;

Re: "Undefined offset" notices driving me crazy

Posted: Tue Jun 24, 2008 4:31 am
by onion2k
It's exactly the same as your "$rows1=mysql_fetch_array($result1)" except it returns the row as an object instead of an array. I prefer it, and it means that if you ever switch to using an abstraction library like ADODB or ADODB Lite you can carry on accessing db content the same way.

The second line puts the 'id' value from the database into the $arrIds array.