开发者

Error detection in multiple queries in MySQL using PHP

开发者 https://www.devze.com 2022-12-08 12:31 出处:网络
I have a wrapper php class for mysqli which amongst other things needs to be able to handle multiple queries (using mysqli::multi_query)开发者_Python百科. It works but I can\'t find how I\'m supposed

I have a wrapper php class for mysqli which amongst other things needs to be able to handle multiple queries (using mysqli::multi_query)开发者_Python百科. 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?


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; 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消