简体   繁体   中英

PHP/MySQL Count() Issue

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.";
}
?>

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) { ... }
}

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" }
}

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];

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM