我遇到了一个不寻常的问题,一个我真的无法相信的问题实际上正在发生,但它正在发生并且无法找到解决方法。如果有人知道我做错了什么,请帮助我。
我需要一个数组$EmpExists=array();
,它在检查数据库中是否有与之关联的员工的所有业务后显示结果。我使用PHP从数据库中获取一些值,并检查值是否为空。我有以下表格和数据行:
Table `EmployeeList`
Columns EmpID BusinessID
Row 1 `emp-000001`,`business-000001`
Row 2 `emp-000002`,`business-000002`
Table `BusinessList`
Columns BusinessID
Row 1 `business-000001`
Row 2 `business-000002`
Row 3 `business-000003`
我正在使用以下php代码来调用商家列表:
<?php
$BusinessIDforthis = array();
$select_BusinessIDs = "SELECT BusinessID FROM BusinessList ORDER BY BusinessID ASC;";
$select_BusinessIDs_query = mysqli_query($connection, $select_BusinessIDs);
if (!$select_BusinessIDs_query) {
die ("Database query for searching BusinessID failed.");
}
while ($BusinessIDs_array = mysqli_fetch_assoc($select_BusinessIDs_query)) {
$BusinessIDforthis[] = $BusinessIDs_array["BusinessID"];
}
这给了我一个BusinessID
的数组,然后我使用以下php代码来获取EmpID
的{{1}}员工
BusinessID
现在$EmpID = '';
$EmpExists = array();
$EmpIDRecord = array();
foreach ($BusinessIDforthis as $x) {
$select_EmpID = "SELECT EmpID FROM EmployeeList WHERE BusinessID='{$x}';";
$select_EmpID_query = mysqli_query($connection, $select_EmpID);
if (!$select_EmpID_query) {
die ("Database query for searching EmpID failed.");
}
while ($EmpID_array = mysqli_fetch_assoc($select_EmpID_query)) {
$EmpID = $EmpID_array["EmpID"];
if (empty($EmpID)) {
array_push($EmpExists, 'EmpNotExists');
array_push($EmpIDRecord, 'Employee does not exist.');
} else {
array_push($EmpExists, 'EmpExists');
array_push($EmpIDRecord, $EmpID);
}
$EmpID = '';
}
}
显示一个包含以下答案的数组:
$EmpExists=array();
和Array
(
[0]=> EmpExists
[1]=> EmpExists
[2]=> EmpExists
)
显示一个包含以下答案的数组:
$EmpIDRecord=array();
和Array
(
[0]=> emp-000001
[1]=> emp-000002
)
显示一个包含以下答案的数组:
$BusinessIDforthis=array();
问题是,我需要它将Array
(
[0]=> business-000001
[1]=> business-000002
[2]=> business-000003
)
中的最后一项显示为$EmpExists=array();
,因为来自EmpNotExists
的最后一个BusinessID
没有任何内容在BusinessList
中记录。我怎么能这样做,请尽可能指导我?
答案 0 :(得分:1)
$select_EmpID_query = mysqli_query($connection, $select_EmpID);
将始终返回true,除非资源错误,这意味着它将循环次数与其真实次数(BusinessList = 3次循环中的3行)。您应该使用mysqli_num_rows()
代替。
真的,你应该使用JOIN来获取你的数据:
SELECT * FROM BusinessList AS BL
LEFT JOIN EmployeeList as EL ON BL.BusinessID = EL.BusinessID
ORDER BY BL.BusinessID ASC
另外,$EmpID = $EmpID_array["EmployeeID"];
这不应该是$EmpID = $EmpID_array["EmpID"];
?
答案 1 :(得分:0)
要检查上述问题的空状态mysqli_fetch_assoc
总是产生true
,可以使用mysqli_num_rows
来代替上述问题,方法如下:
$EmpID = '';
$EmpExists = array();
$EmpIDRecord = array();
foreach ($BusinessIDforthis as $x) {
$select_EmpID = "SELECT EmpID FROM EmployeeList WHERE BusinessID='{$x}';";
$select_EmpID_query = mysqli_query($connection, $select_EmpID);
if (!$select_EmpID_query) {die ("Database query for searching EmpID failed.");}
else
{$EmpIDrows = mysqli_num_rows($select_EmpID_query);
if ($EmpIDrows===0) {
array_push($EmpExists, 'EmpNotExists');
array_push($EmpIDRecord, 'Employee does not exist.');
} else {
array_push($EmpExists, 'EmpExists');
array_push($EmpIDRecord, $EmpID);
}
$EmpIDrows= '';
}
}