Search code examples
phpmysqlarraysis-empty

php empty() is not working correctly


I am having an unusual issue, one I really cannot believe is actually happening, but it is happening and am not able to find the way out of it. Please help me if someone knows what am I doing wrong.

I need to have an array $EmpExists=array();, which shows results after checking the database for all the businesses that do they have employees associated to them or not. I am using php to get a few values out of database and am checking if the value is empty or not. I have following Tables and Rows of data in them:

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`

I am using following php code to call list of businesses:

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

This gives me an array of BusinessID and then I use following php code to get the EmpID of employees for the 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 = '';
    }
}

Now $EmpExists=array(); shows an array with following answers:

Array
(
[0]=> EmpExists
[1]=> EmpExists
[2]=> EmpExists
)

and $EmpIDRecord=array(); shows an array with following answers:

Array
(
[0]=> emp-000001
[1]=> emp-000002
)

and $BusinessIDforthis=array(); shows an array with following answers:

Array
(
[0]=> business-000001
[1]=> business-000002
[2]=> business-000003
)

The issue is that I need it to show the last item inside the $EmpExists=array(); to be EmpNotExists because the last BusinessID from BusinessList does not have any record of it in EmployeeList. How can I do this right, please guide me if possible?


Solution

  • To check the empty status of above question where mysqli_fetch_assocdoes always yield a true one can use mysqli_num_rows instead for the above question, in the following way:

    $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= '';
       }
       }