我正在嘗試爲使用PHP和MySQL的客戶端創建類註冊系統。我已經建立了數據庫和表格,並且該部分工作得很好,但是,客戶要求在註冊後,如果有3名或更少的學生註冊以警告該類別可能無法運行。PHP/MySQL Count()問題
我正在嘗試使用count()函數以及從註冊PHP腳本中設置的cookie中傳遞動態變量。不過,我遇到了障礙。我似乎無法得到count()函數來實際計數行。我的選擇聲明如下。任何幫助將不勝感激。
$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.";
}
?>
您不需要同時使用MySQL的'COUNT()'過程和PHP的'mysql_num_rows()';使用一個或另一個。另外,'echo $ count'是什麼給你的? – Bojangles
您應該更加小心地命名變量。你用變量和字段名稱做了三件不同的事情,你們都稱之爲數字。使用像count_classes或$ count_rows之類的更具描述性的內容來避免令人眼花繚亂。 – ty812