Jump to content

Undefined index error when using Count()


nashsaint

Recommended Posts

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

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>

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>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.