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. Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/ 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. Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/#findComment-789221 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> Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/#findComment-789242 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> Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/#findComment-789328 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 Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/#findComment-789336 Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 It's ORDER BY not SORT BY Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/#findComment-789343 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. Link to comment https://forums.phpfreaks.com/topic/150284-undefined-index-error-when-using-count/#findComment-789354 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.