PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
$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);
}
}
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';".
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.
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
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.
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'];
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
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.