11

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;
Community
  • 1
  • 1
Charkel
  • 308
  • 1
  • 4
  • 15

7 Answers7

19

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);
}
svens
  • 11,438
  • 6
  • 36
  • 55
4

Try this:

$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
$count = mysql_result($rt, 0, 0);
echo $count;
Pekkasso
  • 419
  • 2
  • 8
4

In PHP, resources are returned from certain functions so that they can be passed to other related functions. Examples include database connections, database query results, file-handles, etc.

According to the documentation on mysql_query(), a SELECT query returns a resource. You can take that resource and pass it to a number of different functions. To retrieve a count of the rows, you can use mysql_num_rows(), to retrieve the results of the query, you can use either mysql_fetch_array(), mysql_fetch_assoc() or mysql_fetch_object().

A normal pattern for dealing with database results will look something like this:

$result = mysql_query("SELECT * FROM persons"); // run query against database
$count = mysql_num_rows($result); // retrieve a count of the rows in the previous query
while ($row = mysql_fetch_assoc($result)) { // loop through all the rows in the resultset
    // use $row['column_name'] to access columns in your resultset
}

From your example above:

$result = mysql_query("SELECT COUNT(*) AS num FROM persons"); // run query against db
$row = mysql_fetch_assoc($result); // retrieve the 1 (and only) row
$count = $row['num']; // we needed to alias the COUNT(*) column as `num`
Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
3

mysql_query() doesn't return a value, it returns a resource (see here in the manual).

The returned result resource should be passed to another function for dealing with result tables (like mysql_fetch_array() or mysql_fetch_assoc()), to access the returned data.

Example based on your initial code:

$rt=mysql_query("SELECT COUNT(*) FROM persons");
while($row = mysql_fetch_assoc($rt)) {
  var_dump($row);
}
Bjoern
  • 15,934
  • 4
  • 43
  • 48
1

mysql_query returns a resource object. You need to fetch rows from it first (mysql_fetch_row).

mingos
  • 23,778
  • 12
  • 70
  • 107
1

Straight from PHP.net.......

"For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."

Valandres
  • 69
  • 1
  • 10
1

From the documentation on mysql_query:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235