Salis Posted August 2, 2007 Share Posted August 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63069-solved-select-count-help/ Share on other sites More sharing options...
akitchin Posted August 2, 2007 Share Posted August 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63069-solved-select-count-help/#findComment-314090 Share on other sites More sharing options...
Salis Posted August 2, 2007 Author Share Posted August 2, 2007 Thanks for clearing that up for me. From my understanding count returns the number of rows rather than data. Which is better to say than "select * from" and "mysql_num_rows" Quote Link to comment https://forums.phpfreaks.com/topic/63069-solved-select-count-help/#findComment-314110 Share on other sites More sharing options...
akitchin Posted August 2, 2007 Share Posted August 2, 2007 that's exactly right - no need to grab that data unless you're actually going to use it (otherwise that's a terrible use of resources). glad to see another convert. Quote Link to comment https://forums.phpfreaks.com/topic/63069-solved-select-count-help/#findComment-314118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.