Re: Array?
| From: | John Holmes | Date: | Wed, 25 Aug 2004 18:43:45 +0000 |
| Subject: | Re: Array? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-194709@lists.php.net to get a copy of this message | ||
From: "Nathan Mealey" <nathan@cycle-smart.com>
I cannot seem to access elements inside of the array $things in the code below. All of the errors say "Undefined offset: 7 in /Library/WebServer/Documents/lis/check.php on line 26" I don't see why... The code is: $q = "select * from users where email='".$email."' and password='".$pass."'"; $results = mysql_query($q); if ($results) { while ($list=mysql_fetch_assoc($results)) { $things[]=$list; } } //$things[7] is a MySQL timestamp column named "last_login" $date = strtotime($things[7]);You're creating a two dimensional array. $things[0][7] is the first row, eight column, which is what you're after if this only returns one row. if you want to use $things[7], then within your while loop, use $things = $list;, for example. Although, if you're only getting one row, there's no need for a while loop and you can just say $things = mysql_fetch_assoc($results); Oh, one more thing. fetch_assoc() returns an array with the keys named after the columns, not as numbers. So $things[0]['last_login'] or $things['last_login'] is what you're after. Or use mysql_fetch_row(), which will return keys as number... or use mysql_fetch_array(), which will do both. have fun. Don't break anything. ---John Holmes...