PHP list issue (Undefined offset :)

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

When I retrieve a record, I find it, but when it comes to returning results, I am puzzled. This is where I am stuck.

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

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

And when I run the list function there (for debugging purposes explicitly), 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 causes this?

+6
source share
5 answers

Replace:

mysql_fetch_assoc($RS)

from:

mysql_fetch_array($RS, MYSQL_NUM)

then it should work, because the list function is trying to access the array using the number keys.

+5
source

I think the answer lies somewhere inside this;

list () only works on numeric arrays and assumes that numeric indices start at 0.

: (

+4
source

You used mysql_fetch_assoc, so the resultant array for each row has the data under the key by the column name, while "list" tries to map the variable values ​​to numerical indices. You can use mysql_fetch_array instead.

+1
source

Instead, you can use extract() here; ( here .)

+1
source

$ categ = val1 | mean2

list ($ one, $ two, three $) = @ split ('[|]', $ Category);

If you try to list a value that is not available, it will return an Undefined Offset error.

Here the error will be Undefined Offset 2.

Since when splitting $ categ, it will have only two values, if you try to access the third value, then it will return an error.

0
source

Source: https://habr.com/ru/post/911824/


All Articles