nashsaint Posted March 20, 2009 Share Posted March 20, 2009 Hi, I have a simple query to count names of their occurence and then sort by count. Here's the query. SELECT acctm_name, COUNT( acctm_name ) AS totalCount FROM job_enquiry GROUP BY acctm_name ORDER BY totalCount DESC The query works perfectly if i run it inside phpmyAdmin but produced this error when running the actual php page: Notice: Undefined index: COUNT( acctm_name ) in C:\wamp\www\enquiries\report.php on line 80 Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 This is not SQL error, but PHP notice. Post the relevant PHP code. Quote Link to comment Share on other sites More sharing options...
nashsaint Posted March 20, 2009 Author Share Posted March 20, 2009 This is the query: mysql_select_db($database_enquiry_connect, $enquiry_connect); $query_countAcctm = "SELECT acctm_name, COUNT( acctm_name ) FROM job_enquiry GROUP BY acctm_name"; $query_limit_countAcctm = sprintf("%s LIMIT %d, %d", $query_countAcctm, $startRow_countAcctm, $maxRows_countAcctm); $countAcctm = mysql_query($query_limit_countAcctm, $enquiry_connect) or die(mysql_error()); $row_countAcctm = mysql_fetch_assoc($countAcctm); if (isset($_GET['totalRows_countAcctm'])) { $totalRows_countAcctm = $_GET['totalRows_countAcctm']; } else { $all_countAcctm = mysql_query($query_countAcctm); $totalRows_countAcctm = mysql_num_rows($all_countAcctm); } $totalPages_countAcctm = ceil($totalRows_countAcctm/$maxRows_countAcctm)-1; ?> This is the form that populates the query: <form id="form1" name="form1" method="post" action=""> <table border="0" cellpadding="3" cellspacing="1" id="archiveTblSmall"> <tr> <th>Names</th> <th>Totals</th> </tr> <?php do { ?> <tr> <td><?php echo $row_countAcctm['acctm_name']; ?></td> <td><?php echo $row_countAcctm['COUNT( acctm_name )']; ?></td> </tr> <?php } while ($row_countAcctm = mysql_fetch_assoc($countAcctm)); ?> </table> </form> Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 Change your query to SELECT acctm_name, COUNT( acctm_name ) AS count_acctm_name FROM job_enquiry GROUP BY acctm_name And this piece of code: <td><?php echo $row_countAcctm['COUNT( acctm_name )']; ?></td> to <td><?php echo $row_countAcctm['count_acctm_name']; ?></td> Quote Link to comment Share on other sites More sharing options...
nashsaint Posted March 20, 2009 Author Share Posted March 20, 2009 That works well. Have one more problem though, how can i sort the result by count? I tried this code but didn't work, produced same problem. SELECT acctm_name, COUNT( acctm_name ) AS count_acctm_name FROM job_enquiry GROUP BY acctm_name SORT BY count_acctm_name ASC Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 It's ORDER BY not SORT BY Quote Link to comment Share on other sites More sharing options...
nashsaint Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks a lot Mchl... works great now. Quote Link to comment 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.