limitphp Posted January 29, 2009 Share Posted January 29, 2009 If I have a simple query: $query = "select count(*) from table"; $mysql_query($query) or die (mysql_error()); What is the simplest way to display the actual number of rows returned? since I'm not actually grabbing a row, I'm not sure how to display the count. Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/ Share on other sites More sharing options...
GraphiX Posted January 29, 2009 Share Posted January 29, 2009 $sql = "SELECT * FROM `table`"; $query = mysql_query($sql) or die(mysql_error()); echo mysql_num_rows($query) I think that would work. The way you're doing it would also work. Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-749994 Share on other sites More sharing options...
redarrow Posted January 29, 2009 Share Posted January 29, 2009 Can be done this way as well mate. <?php //database connection. /// select with count. $query = "select count(*) as num from table"; //query select. $mysql_query($query) or die (mysql_error()); //loop query. while($row=mysql_fetch_array($mysql_query)){ //echo result. echo $row['num']; } ?> and this way. <?php //database connection. /// select with count. $query = "select count(*) as num from table"; //query select. $mysql_query($query) or die (mysql_error()); //echo result. echo $mysql_query['num']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750003 Share on other sites More sharing options...
limitphp Posted January 29, 2009 Author Share Posted January 29, 2009 $sql = "SELECT * FROM `table`"; $query = mysql_query($sql) or die(mysql_error()); echo mysql_num_rows($query) I think that would work. The way you're doing it would also work. thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750005 Share on other sites More sharing options...
GraphiX Posted January 29, 2009 Share Posted January 29, 2009 would display as a number I think, never used that method though. Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750006 Share on other sites More sharing options...
trq Posted January 29, 2009 Share Posted January 29, 2009 $query = "select count(*) from table"; $result = mysql_query($query); echo mysql_result($result,0); Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750010 Share on other sites More sharing options...
limitphp Posted January 29, 2009 Author Share Posted January 29, 2009 $query = "select count(*) from table"; $result = mysql_query($query); echo mysql_result($result,0); whoa.....awesome...thank you. Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750012 Share on other sites More sharing options...
premiso Posted January 29, 2009 Share Posted January 29, 2009 If you want just the count, this is the better/preferred method: <?php $sql = "SELECT count(id) as cnt FROM `table`"; $query = mysql_query($sql) or die(mysql_error()); $cnt = mysql_result($query, 0); echo $cnt; ?> Do not use count(*) as it is slower and less efficient. Defining a column is much faster and you should generally use the index/primary key column. EDIT: Sort of beaten to it by Thorpe, but yea, since I do not use count(*) I figured this was ok to post Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750013 Share on other sites More sharing options...
limitphp Posted January 29, 2009 Author Share Posted January 29, 2009 If you want just the count, this is the better/preferred method: <?php $sql = "SELECT count(id) as cnt FROM `table`"; $query = mysql_query($sql) or die(mysql_error()); $cnt = mysql_result($query, 0); echo $cnt; ?> Do not use count(*) as it is slower and less efficient. Defining a column is much faster and you should generally use the index/primary key column. EDIT: Sort of beaten to it by Thorpe, but yea, since I do not use count(*) I figured this was ok to post Oh, ok.....thank you. yeah, that was exactly what I wanted...just the count.... thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/143027-solved-simplest-way-to-display-select-count/#findComment-750021 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.