why do i get resource id 4 when i apply print r to an array in php

why do i get resource id 4 when i apply print r to an array in php  using -'php,mysql,arrays'

  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());

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

After I added

{ print_r($row); }

I just got []

What's wrong?

asked Oct 19, 2015 by abhimca2006
0 votes

4 Answers

0 votes

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. :)

answered Oct 19, 2015 by vickeykumar66
0 votes

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"];
answered Oct 19, 2015 by loknath.ganji
0 votes

$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...




answered Oct 19, 2015 by r3tt
0 votes

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


answered Oct 19, 2015 by ankitarajoria4