'compare 2 results of PDO
In my web app, user can write any SQL SELECT statement into a code editor. Then I am checking if his solution is correct by comparing his result set with my own correct result set.
I have 2 results from those queries:
// user SQL, for example "SELECT col_1, col_2 FROM table ORDER BY col_1";
$query1 = $con->prepare($userSql);"
$query1->execute();
// correct SQL
$query2 = $con->prepare("SELECT col_1, col_2 FROM table;");
$query2->execute();
I need to compare results from those objects - more specifically, I need to know, whether those 2 result sets are identical.
Is there any other way of comparing result sets?
My solution is that I am comparing fetched rows. BUT: If the first query e.g. doesn't contain an ORDER BY clause, I still get the same result set as the second query. However, I don't want to depend on DBMS hence the result set is not deterministic.
Solution 1:[1]
You can use the function array_diff to find difference for the SQL query result.
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
// Array
// (
// [1] => blue
// )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
