Click to See Complete Forum and Search --> : MySQL SELECT COUNT(*)...


bdg1983
06-15-2001, 02:21 AM
this:


$number = mysql_query("SELECT COUNT(id) FROM table");
echo $number;


Returns:

"Resource id #3"

Shouldn't it return the number of rows in the ID field?

Thanks.

Alex

Sweede
06-15-2001, 02:28 AM
$result = mysql_query("SELECT COUNT(id) AS id FROM table where id < 1000");
$number = mysql_result($result,'0','id');
echo $number;

is the correct way.

mysql_query returns a result pointer that you use in
mysql_result, mysql_num_rows, mysql_num_fields, mysql_Fetch_object,mysql_fetch_array, mysql_fetch_row


well, you get the point.

bdg1983
06-15-2001, 04:02 AM
Ah, you have shown me the light. Gracias.

Salmon
06-15-2001, 10:39 AM
Also, 'COUNT(*)' is faster than 'COUNT(column_name)'.