Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this return Resource id #2? [duplicate]

Tags:

sql

php

mysql

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

I am new at php and SQL and I'm trying to make the php page list the numbers of enries in the table. I'm using this code but it returns Resource id #2:

$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
echo "<h1>Number:</h1>".$rt;
like image 438
Charkel Avatar asked Jan 25 '11 14:01

Charkel


1 Answers

Because you get a mysql ressource when you do a mysql_query().

Use something like mysql_fetch_assoc() to get the next row. It returns an array with the column names as indices. In your case it's probably COUNT(*).

Here's a fix and some minor improvements of your snippet:

$rt = mysql_query("SELECT COUNT(*) FROM persons") or die(mysql_error());
$row = mysql_fetch_row($rt);
if($row)
    echo "<h1>Number:</h1>" . $row[0];

If you need to get all rows of the resultset use this snippet:

while($row = mysql_fetch_assoc($rt)) {
    var_dump($row);
}
like image 97
svens Avatar answered Nov 07 '22 23:11

svens