...

View Full Version : PHP count() returning 1 on mysql result set, even when empty!



jazzdrive3
08-31-2009, 06:18 AM
I'm at a complete loss. How do I count the number of results a sql call returned, because count() sure isn't doing it.

Anyone know what's going on?

Thanks!

abduraooft
08-31-2009, 06:27 AM
Please post your code. Are you executing a count() function in your query?

Phil Jackson
08-31-2009, 08:35 AM
$query = ("SELECT * FROM `table_name` WHERE name='johnDoe'");
$amountOfJohnDoes = mysql_num_rows($query);

abduraooft
08-31-2009, 08:46 AM
SELECT * FROM `table_name` WHERE name='johnDoe'"; No. That's very inefficient for that purpose! Use something like,

$result =mysql_query ("SELECT count(*) as count FROM `table_name` WHERE name='johnDoe'");
echo $value=mysql_result($result,0);

SKDevelopment
08-31-2009, 11:13 AM
No. That's very inefficient for that purpose! Use something like,

$result =mysql_query ("SELECT count(*) as count FROM `table_name` WHERE name='johnDoe'");
echo $value=mysql_result($result,0);
I think Phil Jackson simply meant using mysql_num_rows() (http://php.net/mysql_num_rows) to get the number of rows returned. The OP did not ask how to get number of all records in a table. It seems he needed the number of rows in the returned recordset. For which mysql_num_rows() (http://php.net/mysql_num_rows) is really the best solution.

Ashif
08-31-2009, 11:35 AM
$query = ("SELECT * FROM `table_name` WHERE name='johnDoe'");
$amountOfJohnDoes = mysql_num_rows($query);



before using the mysql_num_rows, you need to execute the query to get the record set. then count the number of records in the record set.

Scriptet
08-31-2009, 01:28 PM
before using the mysql_num_rows, you need to execute the query to get the record set. then count the number of records in the record set.

Yes just to clarify that would mean:



//Query Txt
$query = "SELECT * FROM `table_name` WHERE name='johnDoe'";

//Actually Execute The Query, and store the result set in $result
$result = mysql_query($query) or die(mysql_error());

//Now you can perform num_rows on the result resource :)
$amountOfJohnDoes = mysql_num_rows($result);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum