1

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?

4
  • @RiggsFolly Could you see what am I doing wrong in my code above then? Commented Jan 24, 2017 at 21:01
  • I cant' even read this code. What about some indentation here? Commented Jan 24, 2017 at 21:06
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST or $_GET data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Jan 24, 2017 at 22:44
  • Thanks I shall keep in mind this. Commented Jan 24, 2017 at 22:52

2 Answers 2

1

$select_EmpID_query = mysqli_query($connection, $select_EmpID); will always return true unless theres a resource error which means that it will loop as many times as its true (3 rows from BusinessList = 3 loops). You should use mysqli_num_rows() instead.

Really, you should be using JOINs to obtain your data as such:

SELECT * FROM BusinessList AS BL
LEFT JOIN EmployeeList as EL ON BL.BusinessID = EL.BusinessID
ORDER BY BL.BusinessID ASC

Also, shouldnt $EmpID = $EmpID_array["EmployeeID"]; this be $EmpID = $EmpID_array["EmpID"];?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks friend, I didn't knew this.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.