Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP list issue ( Undefined offset: )

Tags:

php

list

Original SQL query is this; SELECT id,post_title,post_date FROM wp_posts where id='1'

When I retrieve the record, I am finding it but when it comes to returning the results, I am puzzled. Here is the where I got stuck.

   while ($row = mysql_fetch_assoc($RS)) :
      print_r ($row); 
      list($id,$post_title,$post_date) = $row;
   endwhile;

print_r ($row) outputs this; Array ( [ID] => 1 [post_title] => Hello world! [post_date] => 2012-03-27 03:28:27 )

And when I run the list function in there ( for debug purposes obviously ), I get this;

Notice: Undefined offset: 2 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 1 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 0 in F:\inetpub\wwwroot\whatever\sql.php on line 147

What's causing this?

like image 228
Average Joe Avatar asked Mar 28 '12 01:03

Average Joe


1 Answers

Replace:

mysql_fetch_assoc($RS)

with:

mysql_fetch_array($RS, MYSQL_NUM)

then it should work, because the list function trys to access the array using numeric keys.

like image 125
stewe Avatar answered Oct 31 '22 21:10

stewe