Why does this return resource identifier # 2?

Possible duplicate:
How is the “echo” of “Resource Identifier # 6” from a MySql response in PHP?

I am new to php and SQL, and I am trying to make a list of php pages by the number of enries in the table. I use this code, but it returns resource identifier # 2:

$rt=mysql_query("SELECT COUNT(*) FROM persons"); echo mysql_error(); echo "<h1>Number:</h1>".$rt; 
+8
sql php mysql
source share
7 answers

Since you get mysql ressource when you execute mysql_query() .

Use the following mysql_fetch_assoc() command to get the next line. It returns an array with column names as indexes. In your case, it is possible COUNT(*) .

Here is a fix and some minor improvements to 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 want all rows in the result set to use this snippet:

 while($row = mysql_fetch_assoc($rt)) { var_dump($row); } 
+17
source share

Try the following:

 $rt=mysql_query("SELECT COUNT(*) FROM persons"); echo mysql_error(); $count = mysql_result($rt, 0, 0); echo $count; 
+3
source share

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 descriptors, etc.

According to the documentation on mysql_query() the SELECT query returns a resource. You can take this resource and pass it to several different functions. To get the row count, you can use mysql_num_rows() to get the query results, you can use either mysql_fetch_array() , mysql_fetch_assoc() or mysql_fetch_object() .

A normal template for processing 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 the above example:

 $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` 
+3
source share

mysql_query() does not return a value, it returns a resource (see here in the manual).

The returned result resource must be passed to another function to process the result tables (for example, mysql_fetch_array () or mysql_fetch_assoc () ) in order to access the returned data.

An example based on the source code:

 $rt=mysql_query("SELECT COUNT(*) FROM persons"); while($row = mysql_fetch_assoc($rt)) { var_dump($row); } 
+2
source share

mysql_query returns a resource object. First you need to get the rows from them ( mysql_fetch_row ).

+1
source share

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

"For SELECT, SHOW, DESCRIBE, EXPLAIN, and other statements that return a result set, mysql_query () returns the resource with success or FALSE on error."

+1
source share

From the mysql_query documentation:

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

+1
source share

All Articles