When I run this query `
SELECT
id
FROMbckoff
WHEREleft
= 3;
` in phpmyAdmin, I get the correct response
MySQL returned an empty result set (i.e. zero rows).
H开发者_如何学Cowever, when I run the same query through my PHP code using mysql_query('the above query').. then I get "Resource ID#5"
or "Resource ID#6"
and so on..
How do I get the empty result set (or zero rows) in PHP ?
mysql_num_rows is the answer. This function returns the number of rows affected by a executed query.
$query = "SELECT id FROM bckoff WHERE left = 3";
$result = mysql_query($query);
echo mysql_num_rows($result);
When you execute mysql_query($query)
it executes the query and puts it in a resource. This resource can be read by different mysql-functions (like mysql_num_rows). For a complete overview of all MySQL functions have a look at http://nl.php.net/manual/en/ref.mysql.php
Note: Extension used in above code is deprecated as of PHP 5.5.0, Use MySQLi or PDO_MySQL extension. So instead of mysql_num_rows use mysqli_num_rows()
You can use mysql_num_rows
function as:
$result = mysql_query("SELECT id FROM bckoff WHERE left = 3");
$num_rows = mysql_num_rows($result);
// $num_rows will be 0.
You need to use a mysql_fetch_* function to retrieve the results. Look here
Mysql Fetch Functions
There is a mysql_num_rows
function that you can call on the $result
returned by mysql_query("SELECT ...")
.
You might look into the MySQLi extension instead. It's a big improvement over the MySQL driver, and allows you to use prepared statements and bind parameters among other things, and I find it much more comfortable to use. You can look at the examples on the documentation page for num_rows
.
精彩评论