为什么会出现此 PHP 错误:"Strict standards: mysqli::next_result(): There is no next result set."?

Why this PHP error occurs: "Strict standards: mysqli::next_result(): There is no next result set."?

我有代码,基本上是 php.net 代码的副本,但由于某种原因它不起作用。这是 php.net:

上的代码
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

我做的第一个改变是连接:

$mysqli = new mysqli("localhost", "root", "", "fanfiction");

我做的第二个改变是查询:

$query = "SELECT first FROM tests;";
$query .= "SELECT second FROM tests;";
$query .= "SELECT third FROM tests;";
$query .= "SELECT fourth FROM tests";

编辑:我修改后的完整代码

<?php
$mysqli = new mysqli("localhost", "root", "", "fanfiction");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT first FROM tests;";
$query .= "SELECT second FROM tests;";
$query .= "SELECT third FROM tests;";
$query .= "SELECT fourth FROM tests";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

我得到的错误:

Strict standards: mysqli::next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in address on line line number

我在网上搜索了一个解决方案,尤其是在 Whosebug 上,但没有找到有用的解决方案。我找到的大多数解决方案都是这两个之一:

试试

} while ($mysqli->more_results() && $mysqli->next_result());

sscce:

<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL|E_STRICT);

$mysqli = new mysqli("localhost", "localonly", "localonly", "test");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query('CREATE TEMPORARY TABLE City (ID int auto_increment, `Name` varchar(32), primary key(ID))') or die($mysqli->error);

$stmt = $mysqli->prepare("INSERT INTO City (`Name`) VALUES (?)") or die($mysqli->error);
$stmt->bind_param('s', $city) or die($stmt->error);
foreach(range('A','Z') as $c) {
    $city = 'city'.$c;
    $stmt->execute() or die($stmt->error);
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if (!$mysqli->multi_query($query)) {
    trigger_error('multi_query failed: '.$mysqli->error, E_USER_ERROR);
}
else {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("'%s'\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->more_results() && $mysqli->next_result());
}

打印

'localonly@localhost'
-----------------
'cityU'
'cityV'
'cityW'
'cityX'
'cityY'

没有warnings/notices。