3

Here's what I'm trying:

if($mysqli->multi_query(file_get_contents('file_that_contains_a_bunch_of.sql'))) {
    do {
        $result = $mysqli->use_result();
        if($mysqli->errno === 0) {
            echo $mysqli->affected_rows.' row(s) affected'.PHP_EOL;
        } else {
            die($mysqli->error);
        }
    } while($mysqli->next_result());
}

But it's aborting the loop early and doesn't print an error message. How do I find out what the error is?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

3 Answers3

6

Peter deleted his answer, but it hinted at what the problem was. I have to put the error check outside of the loop.

Below everything, I need to write

if($mysqli->errno) die($mysqli->error)

The next_result function returns false if there's an error on the next query, so it won't even enter the body of the loop.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
2

It may aborting the loop if you configure somehow mysqli_report, look for string like this

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

in this case, each time you'll try to do this

$mysqli->use_result();

you will have an error generated if there was no result, for example if there was "set", "insert", "update" or "delete" types of query.

Did you find the way to get the error message?

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • 1
    This was a couple years ago, but yes, I think so. See [my answer](http://stackoverflow.com/a/12287843/65387) – mpen Dec 10 '14 at 03:41
  • From my experience the "multi_query" returns true even if some of the queries failed, that is why - there is no point checking it you can eliminate "if" statement. But you can add something: if you use "START TRANSACTION;" and "commit;" in your SQL scripts, you can avoid some problems. I mean, by default PHP does NOT start transaction for "multi_query". – Yevgeniy Afanasyev Dec 10 '14 at 04:53
  • 2
    mysqli::multi_query ONLY Returns FALSE if the first statement failed. from here http://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.multi-query.html – Yevgeniy Afanasyev Dec 10 '14 at 05:43
  • I havn't got the simple answer, so I invented the wheel, please, see my answer on the same question here: http://stackoverflow.com/questions/7395326/how-do-i-ensure-i-caught-all-errors-from-mysqlimulti-query/27394448#27394448 – Yevgeniy Afanasyev Dec 23 '14 at 02:37
-1

use this code it work well

while ($conn->more_results()){
     $conn->next_result();
     echo $conn->error;
}
milad nazari
  • 339
  • 3
  • 5