TheFilmGod Posted October 17, 2007 Share Posted October 17, 2007 I'm good at php but haven't coded anything in mysql/php for overa month now. I hope you can help me. What I essentially want to do, is calculate the number of "e" / "u" / "p" in each column. I don't need helping echoing the results, I just need help in the mysql coding part. How do I make php/mysql count the number of "e" in a column. Then count the number of "u"? Thanks for all your help in advance! Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/ Share on other sites More sharing options...
Asperon Posted October 17, 2007 Share Posted October 17, 2007 more info please, do you mean count the number of times the letter "e" shows up or do you mean how many rows in column "e" contain data Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371227 Share on other sites More sharing options...
TheFilmGod Posted October 17, 2007 Author Share Posted October 17, 2007 Thanks for the reply. Each row has a char limit of 1. That is either "e" "u" or "p". I want to count how many rows in each column contain "e". Sample Member1 Member 2 Meeting1 u u Meeting2 e e Meeting3 p u Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371231 Share on other sites More sharing options...
pocobueno1388 Posted October 17, 2007 Share Posted October 17, 2007 <?php $query = "SELECT COUNT(*) FROM table WHERE Member1='e'"; $result = mysql_query($query)or die(mysql_error()); $num = mysql_num_rows($result); echo "$num results returned"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371234 Share on other sites More sharing options...
TheFilmGod Posted October 17, 2007 Author Share Posted October 17, 2007 <?php $query = "SELECT COUNT(*) FROM table WHERE Member1='e'"; $result = mysql_query($query)or die(mysql_error()); $num = mysql_num_rows($result); echo "$num results returned"; ?> Although this solution would work perfectly, I do have a question. - The query would be very slow since mysql would have to select the whole table each time. I would count e for about 50 columns, then count u for all the columns and finally p. Isn't there another way around it that uses mysql commands Like count() or something? Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371238 Share on other sites More sharing options...
Asperon Posted October 17, 2007 Share Posted October 17, 2007 <?php $eQuery = "SELECT yourColumn FROM yourTable WHERE yourField='e'"; $eResult = mysql_query($eQuery) or die("Query Failed: ".mysql_error()); $eNum = mysql_num_rows($eResult); //number of rows affected by the query ie. the number of fields in that column with "e" ?> or <?php $eQuery = "SELECT COUNT(*) FROM yourTable WHERE yourField='e'"; $eResult = mysql_query($eQuery)or die("Query Failed: ".mysql_error()); $eNum = mysql_num_rows($eResult); ?> if this isn't so helpful =p try http://www.pantz.org/database/mysql/mysqlcommands.shtml for mysql commands Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371239 Share on other sites More sharing options...
trq Posted October 17, 2007 Share Posted October 17, 2007 That will always return 1. Try... <?php $query = "SELECT COUNT(*) AS num FROM table WHERE Member1='e'"; if ($result = mysql_query($query)) { $row = mysql_fetch_assoc($result); echo $row['num'] . " results returned"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371242 Share on other sites More sharing options...
trq Posted October 17, 2007 Share Posted October 17, 2007 I would count e for about 50 columns, then count u for all the columns and finally p. Isn't there another way around it that uses mysql commands Like count() or something? Sounds to me like the database schema wasn't designed too well in the first place. Unfortunately, your pretty much stuff with the solution I just provided. Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371244 Share on other sites More sharing options...
TheFilmGod Posted October 17, 2007 Author Share Posted October 17, 2007 That will always return 1. Try... <?php $query = "SELECT COUNT(*) AS num FROM table WHERE Member1='e'"; if ($result = mysql_query($query)) { $row = mysql_fetch_assoc($result); echo $row['num'] . " results returned"; } ?> Thanks thorpe! This is exactly what I was looking for. I have one last question... Why do you have an if {}? Is it another alternative to die()... but an even better one, since the script won't terminate? Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371245 Share on other sites More sharing options...
trq Posted October 17, 2007 Share Posted October 17, 2007 Yeah, you pretty much got it. In reality I would use an else for debugging. eg; <?php define("DEBUG",true); // this would be in a config file somewhere. $query = "SELECT COUNT(*) AS num FROM table WHERE Member1='e'"; if ($result = mysql_query($query)) { $row = mysql_fetch_assoc($result); echo $row['num'] . " results returned"; } else { if (DEBUG) { echo mysql_error() . "<br />$query"; } } ?> Something like that.... Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371247 Share on other sites More sharing options...
TheFilmGod Posted October 17, 2007 Author Share Posted October 17, 2007 Nice! Thanks so much Thorpe for your explanation! And thanks for everyone else who helped out! - You do have a nice debugging strategy Thorpe. IMO, die() should be taken out of php because its a horrible habbit for coders and doesn't achieve what you want. - I don't want this to get into a hot debate, but I'm sure there are a few out there that agree with my comment... :-\ Quote Link to comment https://forums.phpfreaks.com/topic/73579-phpmysql-select/#findComment-371256 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.