Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "Resource id #4" when I apply print_r() to an array in PHP? [duplicate]

Tags:

arrays

php

mysql

Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?

Below is the code:

$result=mysql_query("select * from choices where a_id='$taskid'")or die(mysql_error());
print_r($result);

I get "Resource id #4", any idea?

After I added

while($row=mysql_fetch_assoc($result))
{ print_r($row); }

I just got []

What's wrong?

like image 717
Steven Avatar asked Nov 22 '09 04:11

Steven


2 Answers

You are trying to print a mysql resource variable instead of the values contained within the resource it references. You must first try to extract the values you have gotten by using a function such as mysql_fetch_assoc().

You might also try mysql_fetch_array() or mysql_fetch_row(), but I find associative arrays quite nice as they allow you to access their values by the field name as in Mike's example.

like image 192
Kenji Kina Avatar answered Sep 23 '22 01:09

Kenji Kina


mysql_query() does not return an array as explained in the manual. Use mysql_fetch_array(), mysql_fetch_assoc(), or mysql_fetch_row() with your $result. See the link above for more info on how to manipulate query results.

$result = mysql_query('SELECT * FROM table');
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}
like image 23
Mike B Avatar answered Sep 26 '22 01:09

Mike B