为什么会发生此PHP错误:“严格的标准:mysqli :: next_result():没有下一个结果集."? [英] Why this PHP error occurs: "Strict standards: mysqli::next_result(): There is no next result set."?
问题描述
我有代码,基本上是 php.net 的代码,但是由于某些原因它不起作用.这是php.net上的代码:
I have code, which is basically a copy of a php.net's code, but for some reason it does not work. Here is the code on 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();
?>
我得到的错误:
严格的标准: mysqli :: next_result():没有下一个结果集. 请调用 mysqli_more_results()/ mysqli :: more_results()进行检查 是否调用此函数/方法 行号
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
我在网上搜索了一个解决方案,特别是在StackOverflow上,但是没有找到有用的解决方案.我发现的大多数解决方案都是这两种解决方案之一:
I searched a solution over the net, and particularly here on StackOverflow, but I did not find helpful solutions. Most of the solutions I found were one of those two:
- 在此解决方案中, @Hammerite 表示将循环从
do-while
更改为while
.这表明php.net的代码逻辑上有问题,我很难相信.但更重要的是,它对我不起作用. - 在此解决方案中, @mickmackusa 建议在
while
中添加一个条件并将$mysqli->next_result()
更改为$mysqli->next_result() && $mysqli->more_results()
,但是这种解决方案效果不佳.它确实消除了错误,但忽略了最后的结果.
- In this solution,@Hammerite says to change the loop from
do-while
towhile
. This suggest that php.net's code has a problem in its logic, and I find it very hard to believe. But more importantly, it just does not work for me. - In this solution, @mickmackusa suggests to add a condition in the
while
and change$mysqli->next_result()
to$mysqli->next_result() && $mysqli->more_results()
, but this solution do not work quite well. It does indeed removes the error but it omits the last result.
推荐答案
尝试使用
} 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'
没有警告/通知.
这篇关于为什么会发生此PHP错误:“严格的标准:mysqli :: next_result():没有下一个结果集."?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!