2

So, I have two queries which will pull out data from the database. However, I need to use these data to get the array difference by using php. I want to send both employeeName and designation into an array and then get the difference. The below code I have used does not work as expected. Any solution for this matter ?

<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once 'config.php';

$sql1 = "SELECT employeeName, designation FROM t2o_mappings WHERE type = '". $_GET['type'] ."' AND employeeCompany = '".$_GET['employeeCompany']."'"; //System Data;
$sql2 = "SELECT employeeName, designation FROM mappings_view WHERE uNo = '" . $_GET['uNo'] . "' AND uCompany = '" . $_GET['uCompany'] . "' AND type = '". $_GET['type'] ."' AND employeeCompany = '".$_GET['employeeCompany']."'"; //App Data;
//WHERE uNo = '".$_GET['uNo']."' AND uCompany = '".$_GET['uCompany']."'

$result1 = sqlsrv_query($conn, $sql1);
$result2 = sqlsrv_query($conn, $sql2);

$row1 = [];
$row2 = [];

while ($rs1 = sqlsrv_fetch_array($result1, SQLSRV_FETCH_ASSOC)) {

    $row1[] = $rs1['employeeName, designation'];
}

while ($rs2 = sqlsrv_fetch_array($result2, SQLSRV_FETCH_ASSOC)) {

    $row2[] = $rs2['employeeName, designation'];
}

print_r($row1);
print_r($row2);

$HaveSysNoApp = array_diff($row1, $row2);  //Have in System, Not in App
$HaveAppNoSys = array_diff($row2, $row1); //Have in App, Not in System

echo 'HaveSysNoApp';
print_r($HaveSysNoApp);
echo '$HaveAppNoSys';
print_r($HaveAppNoSys);

?>
1
  • "The below code I have used does not work as expected", what does it mean ? What exactly does not work ? Commented May 28, 2017 at 19:18

2 Answers 2

2

You have a syntax problem here

$row1[] = $rs1['employeeName, designation'];

The right way would be :

$row1[] = [
    $rs1['employeeName'],
    $rs1['designation']
];

But array_diff() throws the notice "array to string conversion" because it can only deals with one dimension. The PHP array_diff() documentation contains this note :

Note:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In other words: when the string representation is the same.

Since you have multidimensional arrays to compare, you could use the array_filter() function.

For example :

$HaveSysNoApp = array_filter($row1, function ($item) use ($row2) {
    return !in_array($item, $row2);
});
Sign up to request clarification or add additional context in comments.

3 Comments

the solution you gave is correct, but it doesn't work with the array difference function, the error I get is "Notice: Array to string conversion on line 38"
@AndrewJeromeBernard I edited my answer. Thank you for let me know if it solved your question.
@AndrewJeromeBernard Great ! Please, validate and up-vote my answer. Good luck.
0

You could do it directly in database with this query (the opposite check would be similar):

SELECT system.employeeName, system.designation
FROM t2o_mappings AS system
WHERE system.type = ?
  AND system.employeeCompany = ?
  AND NOT EXISTS (
    SELECT NULL
    FROM mappings_view AS app
    WHERE app.employeeName = system.employeeName
      AND app.designation = system.designation
      AND app.type = system.type
      AND app.uNo = ?
      AND app.uCompany = ?
  )

These question marks make it parametrized query, wich prevents SQL injection (parameter values are not part of the query, so they can't change its meaning). You would call it with additional parameter array (order must match):

$params = [
    $_GET['type'],
    $_GET['employeeCompany'],
    $_GET['uNo'],
    $_GET['uCompany']
];
$haveSysNoAppResult = sqlsrv_query($conn, $sql, $params);

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.