我正在尝试使用一个计数功能来显示有多少个员工的标题为“销售”
if( !$connect)
{
die("ERROR: Cannot connect to database $db on server $server
using user name $user (".mysqli_connect_errno().
", ".mysqli_connect_error().")");
}
else
{
$userQuery = "COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
$result = mysqli_query($connect, $userQuery);
if (!$result)
{
die("Could not successfully run query ($userQuery) from $db: " .
mysqli_error($connect) );
}
if (mysqli_num_rows($result) == 0)
{
print("No records found with query $userQuery");
}
else
{
print("<h1>SALES STAFF REPORT</h1>");
while ($row = mysqli_fetch_assoc($result))
{
print("<p>There are
".$row['COUNT(empID)']."</p>");
}
}
mysqli_close($connect); // close the connection
}
?>
我收到此错误:
您的SQL语法有误。检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的“ WHERE jobTitle ='Sales”人员附近使用“ COUNT(empID)”
在这条线上:
$userQuery = "COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
我不确定自己做错了什么,但会有所帮助。
最终结果应为“有4个销售人员”。
我的教授确实将此内容包括在作业中,不确定是否会有所帮助。
"There are two different MySQL functions that you can use here: you can
modify the SELECT statement to use a MySQL aggregation function,
or you can use the MySQL function that returns the number of records in the
result set. "
最佳答案
声明时,您缺少SELECT
关键字。
你有:$userQuery = "COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
它应该是:$userQuery = "SELECT COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
关于php - SQL计数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47641788/