Jump to content

Recommended Posts

I am trying to write a function which takes the argument and uses it for the WHERE clause. This particular query is for counting the number of records in the data base that match the WHERE clause. For some reason the result keeps coming up as 0.

 


// Create an array of all status names (appear exactly as the values in the database appear)

   $status_names = array('NEW', 'Proofed', 'Approved', 'Printed', 'Inserted', 'Mailed', 'Hold', 'Canceled');

// Create function for grabbing the SQL counts for each field "job_status" with the vlue of "$status_name"
// "job_number" is the unique ID field

   function get_stat_total($status_name) {
      $sql = 'SELECT job_number FROM job_tracking WHERE job_status = "$status_name"';
      $result = mysql_query($sql) or die(mysql_error());
      $num_rows = mysql_num_rows($result);
      return $num_rows;
   }

// call get_stat_total function with argument of "NEW" for testing purposes
   
   get_stat_total("NEW");

 

What I need the array for is my foreach loop:

 


// Create loop for navigation dl
   echo '<dl id="statusNav">';
   foreach ($status_names as $status_name) {
      echo '<dl id="statusNav"><dt><strong>+</strong><a href="job_status?job_status='.$status_name.'" title="'.$status_name.'">'.$status_name.' <em>';
      get_stat_total($status_name);
      echo '</em></a></dt>';
   }
   echo '</dl>';

Link to comment
https://forums.phpfreaks.com/topic/68161-solved-custom-mysql-row-count-function/
Share on other sites

Your query is within single quotes, therefore the variable in your where clause is not being interpolated. Also, a better syntax would be.....

 

function get_stat_total($status_name) {
  $sql = "SELECT job_number FROM job_tracking WHERE job_status = '$status_name'";
  if ($result = mysql_query($sql)) {
    return mysql_num_rows($result);
  }
  return false;
}

 

A function should never (IMO) call die, if you want the script to die on error it should be done in the calling code.

Should I include the db connect in the function?

 

You don't normally need to. Lets do some debugging first....

 

<?php

function get_stat_total($status_name) {
  $sql = "SELECT job_number FROM job_tracking WHERE job_status = '$status_name'";
  if ($result = mysql_query($sql)) {
    return mysql_num_rows($result);
  } else {
    echo mysql_error();
  }
}

?>

I got it.

 

The problem wasn't with the query (well that was an issue too, and thanks for the tips on the syntax), but the real issue was with the calling of the function.

 

I was calling it like: job_stat_total($status_name);

 

When I needed to echo the function. Thank you very much for your help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.