1

I have a wrapper php class for mysqli which amongst other things needs to be able to handle multiple queries (using mysqli::multi_query). It works but I can't find how I'm supposed to get error details when one of the queries fails. This is a simplified version of the code:

$queries=array('SELECT * FROM core_order_items',
                'SELaaaaECT * FROM core_order_items',
                );

$dbConnection = mysqli_connect('localhost', 'siella', 'arifisis', 'mydb');
$dbConnection->set_charset('utf8'); 
$dbConnection->multi_query(implode(';', $queries)) ;

$i=0;
do {
    echo "Query:  " . $queries[$i] . " - ";
    $resultset=$dbConnection->store_result();
    if($resultset===false && strlen($dbConnection->errno)>0) {
        echo "Returned error for query " . $i . "<br>";
    }
    else {
        echo "Returned set for query " . $i . "<br>";
    }
     $i++;
}   
while ($dbConnection->next_result());

mysqli_close($dbConnection);

Notice how of the two SQL queries the first is fine and the second invalid. The result is:

Query: SELECT * FROM core_order_items - Returned set for query 0

What happened to my second query? If I fix it, it shows up, but when an error occurs it's as if it isn't even in the array. How do I get the error no/message?

1 Answer 1

2

mysqli::next_result returns false if there is an error. This example is from the docs

$dbConnection-->multi_query($query); 
do { 
   $dbConnection-->use_result()->close(); 
   echo "Okay\n"; 
} while ($mysqli->next_result()); 

if ($mysqli->errno) { 
   echo "Stopped while retrieving result : ".$mysqli->error; 
} 
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, so it was doing error checking before an error occurred. Then just as an error came up it would stop checking. Wonderful.
It does not work for me. At first - it is not clear what "mysqli_report" flags should I use?
Secondly, I use SQL code: "set @q=1;select @q as max_id from dual1;" and "$mysqli->errno" has no errors for me. When it should be something like this: "[Err] 1146 - Table 'test.dual1' doesn't exist".
Thirdly, "mysqli::next_result" return false when it has no next result, like when you reach the end of the list of your SQL queries - so there should be a way to determine either there was the end of the list or it was an error.

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.