1

I am trying to create a class registration system for a client that utilizes PHP and MySQL. I have the database and table all set up and that part works just fine, however, the client has requested that upon registration, if there are 3 or fewer students enrolled to warn that the class may not run.

I'm trying to use the count() function as well as passing a dynamic variable from a cookie, set from the registration PHP script. However, I've hit a roadblock. I can't seem to get the count() function to actually count the rows. My select statement is below. Any help would be greatly appreciated.

$class = $_COOKIE["class"];

$min_check = "SELECT class_list, COUNT(class_list) as count 
              FROM T_Student WHERE class_list = '$class' 
              GROUP BY class_list 
              HAVING count < 20";
$result = mysql_query($min_check);
$count = mysql_num_rows($result);

if ($count < 4)
{
  echo "IF THERE ARE 3 OR FEWER PEOPLE SIGNED UP FOR THIS CLASS, IT MAY NOT RUN.\n";
  echo "THERE ARE CURRENTLY " . $count . " PEOPLE SIGNED UP.\n";
}
else if ($count > 4)
{
  echo "There are currently " . $count . " people signed up for this class.";
}
?>
2
  • You don't need to use both the MySQL COUNT() procedure and PHP's mysql_num_rows(); use one or the other. Also, what does echo $count give you? Commented Aug 28, 2011 at 22:03
  • You should be a little more careful with naming your variables. You do three different things with variables and field-names you all call count. Use something more descriptive like count_classes or $count_rows to avoid being dazzled. Commented Aug 28, 2011 at 22:03

4 Answers 4

5

Your SQL query is returning a list of the class_list values, along with a count of each specific instance, where there are less than 20 people registered.

$count = mysql_num_rows($result);

...is getting the number of records returned in the resultset, not the alias count value, which is why you aren't seeing the output you expect. You need to read into your resultset to get the value:

while ($row = mysql_fetch_assoc($result)) {
  $count = $row['count'];

  if($count < 4) { ... }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks all for the input. I knew it was something pretty easy that I needed to add, just been looking at it too long to figure it out.
0

The count that you want is returned in the row of the query. the mysql_num_rows will count the rows returned, which is not what you want. Use this instead.

$result = mysql_query($min_check);
$count = mysql_fetch_row($result);
$count = $count[0];

Comments

0

On a first glance, the HAVING count < 20 is unnecessary.

You use the MySQL-count-function, but never retrieve it's value!? Use:

$firstRow = mysql_fetch_row($result);
$count = $firstRow[1]; // 1 indicates the second column (0 being the first)

Comments

0

I don't recommend using known MySQL identifiers like count. It's confusing.

$class = mysql_real_escape_string($_COOKIE["class"]);

$min_check = "SELECT class_list, COUNT(class_list) as mycount 
          FROM T_Student WHERE class_list = '$class' 
          GROUP BY class_list 
          HAVING mycount < 20";

Don't forget to escape the contents of that cookie!

The error is that count is a reserved word. You need to either surround it in backticks `count` or even better, use a different moniker. It's not an error per se, but it's just too confusing.

Next up, you are not actually retrieving the mycount result from the database. I suggest using code something like this:

$result = mysql_query($min_check);
while( $row = mysql_fetch_assoc($result) ) {
    $people_count = $row['mycount'];
    if ($people_count < 4) { echo "this" }
    else { echo "that" }
}

2 Comments

Factually incorrect. "Names of built-in functions are permitted as identifiers but may require care to be used as such. For example, COUNT is acceptable as a column name. " (from the MYSQL documentation, chapter 8.3
Count can be used unquoted like this. Try it and see. Very true about escaping though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.