Jump to content

[SOLVED] SELECT COUNT(*) help


Salis

Recommended Posts

All right, I don't really use count but now that part of my site needs it.. I can't seem to get it to work. I've read tutorials and articles.

 

Shouldn't this work?

<?php

mysql_connect("====","====", "====");
mysql_select_db("====");

$CntQ = mysql_query("SELECT COUNT(*) FROM ex_promo");
echo "Total Rows: " . mysql_num_rows($CntQ) . "<br>";

$ListQ = mysql_query("SELECT ID_PROMO, x_pB64 FROM ex_promo");
while( $ListR = mysql_fetch_array($ListQ) ) {
	echo "x_pB64: {$ListR['x_pB64']} <br>\n";
}

?>

Shouldn't this echo the total number of rows in this table? This is the exact same code as in my page:

http://exopsclan.com/2/chk.php

 

I do have 4 entires and the while statement does post them but the count() doesn't work. Any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/63069-solved-select-count-help/
Share on other sites

negatory - COUNT() will return the total count as a field.  mysql_num_rows() counts the number of rows returned, but since COUNT() just returns one value, mysql_num_rows() will report "1 rows".  you'll need to extract that COUNT() value returned using a fetching function:

 

$CntQ = mysql_query("SELECT COUNT(*) FROM ex_promo");
echo "Total Rows: " . mysql_result($CntQ, 0, 0) . "<br>";

 

because you want to grab the 0th column from the 0th row returned (ie. the very first field of the first row, as counters start at 0) for that COUNT() value returned from the query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.