15

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?

Community
  • 1
  • 1
Steven
  • 24,410
  • 42
  • 108
  • 130
  • 2
    print_r will only accept arrays and objects. If you use var_dump() it will give you information on whatever you give it. – Ben Shelock Nov 22 '09 at 04:33

4 Answers4

21

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.

Community
  • 1
  • 1
Kenji Kina
  • 2,402
  • 3
  • 22
  • 39
17

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"];
}
Mike B
  • 31,886
  • 13
  • 87
  • 111
4

$result is a resource variable returned by mysql_query. More about resource variables: http://php.net/manual/en/language.types.resource.php

You must use other functions such as mysql_fetch_array() or mysql_fetch_assoc() to get the array of the query resultset.

$resultset = array();
$result=mysql_query("select * from choices where a_id='$taskid'") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
  $resultset[] = $row; // fetch each row...
}
mysql_free_result($result); // optional though...

print_r($resultset);

See:

http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.mysql-fetch-assoc.php
http://php.net/manual/en/function.mysql-query.php

mauris
  • 42,982
  • 15
  • 99
  • 131
1

Resources are special variable types used by PHP to track external resources like database connections, file handles, sockets, etc.

kenorb
  • 155,785
  • 88
  • 678
  • 743
leepowers
  • 37,828
  • 23
  • 98
  • 129