Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.