pneudralics Posted November 18, 2008 Share Posted November 18, 2008 I have a table with 5 rows: data1 data1 data1 data1 data2 I want to count only data1. How can I do that? Tried the below but couldn't get it to work $select = 'SELECT * FROM data WHERE data=data1'; if ($result = mysql_query ($select)) { while ($row = mysql_fetch_array ($result)) { $data1 = $row['data']; echo "$data1"; } } Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/ Share on other sites More sharing options...
MasterACE14 Posted November 18, 2008 Share Posted November 18, 2008 $select = 'SELECT COUNT(*) FROM data WHERE data=data1'; Regards, ACE Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692612 Share on other sites More sharing options...
pneudralics Posted November 18, 2008 Author Share Posted November 18, 2008 The if while result didn't work and I got a 0 for the following: $count = mysql_query("SELECT COUNT(data) AS countdata1 FROM data WHERE 'data' = '$data1'"); $num = mysql_fetch_array($count); echo $num['countdata1']; Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692643 Share on other sites More sharing options...
JasonLewis Posted November 18, 2008 Share Posted November 18, 2008 Why don't you just SELECT * FROM data WHERE data='data1' then use mysql_num_rows to get the number? Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692646 Share on other sites More sharing options...
pneudralics Posted November 18, 2008 Author Share Posted November 18, 2008 Why don't you just SELECT * FROM data WHERE data='data1' then use mysql_num_rows to get the number? How would I echo the amount of rows with mysql_num_rows? I know how to echo the field in the while loop but not the number of rows it returns. Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692680 Share on other sites More sharing options...
Maq Posted November 18, 2008 Share Posted November 18, 2008 Read the link to the manual that ProjectFear provided: $sql = mysql_query("SELECT * FROM data WHERE data = 'data1'"); $num_rows = mysql_num_rows($sql); echo $num_rows; Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692684 Share on other sites More sharing options...
pneudralics Posted November 18, 2008 Author Share Posted November 18, 2008 I get the same thing with mysql_num_rows Both returns zero. Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692699 Share on other sites More sharing options...
pneudralics Posted November 18, 2008 Author Share Posted November 18, 2008 Got it. Thanks for the help. I had to take the single quotes from the first ip after WHERE. Quote Link to comment https://forums.phpfreaks.com/topic/133177-solved-how-do-i-count-mysql-rows-with-specific-data/#findComment-692712 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.