gpt4 book ai didi

php - SQL计数错误

转载 作者:行者123 更新时间:2023-11-29 18:08:11 27 4
gpt4 key购买 nike

我正在尝试使用计数函数来显示有多少员工的头衔为“销售”

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

我收到此错误:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COUNT(empID) FROM personnel WHERE jobTitle='Sales'' at line 1

在这一行:

$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 人员 WHERE jobTitle='Sales'";

应该是:$userQuery = "从人员中选择 COUNT(empID) WHERE jobTitle='Sales'";

关于php - SQL计数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47641788/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com