slj90 Posted November 11, 2014 Share Posted November 11, 2014 $result = mysql_query("SELECT * FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result); This displays the total rows in the table. $result = mysql_query("SELECT COUNT(friend_system_accepter) FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result); This displays '1', which is incorrect. I want to echo out the number of rows where 'friend_system_accepter' = $myusername Thanks Quote Link to comment https://forums.phpfreaks.com/topic/292400-count-rows-on-a-select-from-where-result/ Share on other sites More sharing options...
requinix Posted November 11, 2014 Share Posted November 11, 2014 Your query will return to you a single number that counts how many rows there are. Put another way, how do you expect COUNT() to work if the query has to return every single row? Quote Link to comment https://forums.phpfreaks.com/topic/292400-count-rows-on-a-select-from-where-result/#findComment-1496295 Share on other sites More sharing options...
slj90 Posted November 11, 2014 Author Share Posted November 11, 2014 So how would I count only the rows where the data = 'x'? Quote Link to comment https://forums.phpfreaks.com/topic/292400-count-rows-on-a-select-from-where-result/#findComment-1496330 Share on other sites More sharing options...
Barand Posted November 11, 2014 Share Posted November 11, 2014 example $sql = "SELECT COUNT(*) as tot FROM mytable WHERE data = 'x'"; $res = mysql_query($sql); $row = mysql_fetch_assoc($res); echo $row['tot']; // --> number of records NB: Stop using mysql_xxx functions and change to mysqli_xxx or PDO Quote Link to comment https://forums.phpfreaks.com/topic/292400-count-rows-on-a-select-from-where-result/#findComment-1496333 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.