Using a example from elsewhere on SO to better catch 'hiding' errors. While the code below will catch and return an error, is it possible to improve this to report for which query the error occurred?
With the code below, the output is:
Columns: 18
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FRO inventory' at line 1
Code being tested:
$query = "SELECT * FROM orders WHERE location = 'IN' ORDER BY orderNum DESC LIMIT 20;";
$query .= "SELECT * FRO inventory"; // With error
$ord = array();
$invent = array();
if(mysqli_multi_query($link, $query)) {
do {
// fetch results
if($result = mysqli_store_result($link)) {
echo 'Columns: ' . mysqli_field_count($link) . "<br>";
while($row = mysqli_fetch_assoc($result)) {
if(count($row) > 17)
$orders[] = $row;
elseif(count($row) == 6)
$inv[] = $row;
}
}
if(!mysqli_more_results($link))
break;
if(!mysqli_next_result($link)) {
// report error
echo 'Error: ' . mysqli_error($link);
break;
}
} while(true);
mysqli_free_result($result);
}
Here is an approach that will not only improve the quality of your error messages, it will improve the way you handle your result sets.
Error on first query: If your first query tries to access a table that doesn't exist in the nominated database like:
ordersXYZ
Array$rows
will not exist, novar_export()
will occur, and you will see this response:Error on second query: If your first query is successful, but your second query tries to access a non-existent table like:
inventory2
$rows["Orders"]
will hold the desired row data and will bevar_export()
'ed,$row["Inventory"]
will not exist, and you will see this response:No errors: If both queries are error free, your
$rows
array will be filled with the desired data andvar_export()
'ed, and there will be no error response. With the queried data saved in$rows
, you can access what you want from$rows["Orders"]
and$rows["Inventory"]
.Things to note:
You may notice that I am making variable declarations and conditional checks at the same time, this makes the code more DRY.
As my approach uses
implode()
with a semi-colon on theelseif
line, be sure not to add a trailing semi-colon to your queries.This set of queries always returns a result set because all are SELECT queries, if you have a mixed collection of queries that
affect_rows
, you may find some useful information at this link(https://stackoverflow.com/a/22469722/2943403).mysqli_multi_query()
will stop running queries as soon as there is an error. If you are expecting to catch "all" errors, you will discover that there never be more than one.Writing conditional break points like in the OP's question and solution is not advisable. While custom break points may be rightly used in other circumstances, for this case the break points should be positioned inside of the
while()
statement of thedo()
block.A query that returns zero rows will not cause a error message -- it just won't create any subarrays in
$rows
because thewhile()
loop will not be entered.By using the
key()
function, the OP'sif/elseif
condition that counts the columns in each resultset row can be avoided. This is better practice because running a condition on every iteration can become expensive in some cases. Notice that the array pointer is advanced inside of$q
at the end of eachdo()
iteration. This is an additional technique that you will not find on the php manual page; it allowskey()
to work as intended.And, of course, the
<div><pre>var_export()...</pre></div>
lines can be removed from your working code -- it was purely for demonstration.If you are going to run any more queries after this code block that reuse variables, be sure to clear all used variables so that residual data does not interfere. e.g.
$mysqli_error=null; // clear errors
&reset($q); // reset array pointer
.Take heed to this somewhat vague warning at your own discretion: http://php.net/manual/en/mysqli.use-result.php :
Lastly and MOST IMPORTANTLY for security reasons, do not display query or query error information publicly -- you don't want sinister people to see this kind of feedback. Equally important, always protect your queries from injection hacks. If your queries include user-provided data, you need to filter/sanitize the data to death before using it in
mysqli_multi_query()
. In fact when dealing with user input, my very strong recommendation is to move away frommysqli_multi_query()
and use either prepared statements or pdo for your database interactions for a higher level of security.In your do loop, add a counter, each successful mysqli_next_result increment the counter. Once mysqli_next_result returns false, output the counter as well.
To answer my own question and since the documentation is poor, here's a solution that hopefully will help others. What was missing is a way to catch an error on the 1st query. (The hidden actions of myqsqli_multi_query are difficult to understand.)
Now check for entries in $err array.
This works for two queries.
If the error is in the first, the response for 1st query to PHP is the error message, and for the second (which won't fire), a message tattling on the first.
If the error is in the second, the first's response is returned and the 2nd gets the error message.
I'm using associative arrays and nixing array elements[0]. This adds ['Error'] key only if there is a relevant error.
Finally, I'm not the best PHPer, so it's up to you to fix what's ugly.