Sip*_*ipo 0 php mysqli mysqli-multi-query
我有代码,它基本上是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();
?>
Run Code Online (Sandbox Code Playgroud)
我所做的第一个更改是连接:
$mysqli = new mysqli("localhost", "root", "", "fanfiction");
Run Code Online (Sandbox Code Playgroud)
我所做的第二个更改是查询:
$query = "SELECT first FROM tests;";
$query .= "SELECT second FROM tests;";
$query .= "SELECT third FROM tests;";
$query .= "SELECT fourth FROM tests";
Run Code Online (Sandbox Code Playgroud)
编辑:带有我更改的完整代码
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
严格的标准: mysqli::next_result():没有下一个结果集。请调用mysqli_more_results() / mysqli::more_results()来检查是否在 line行号的address 中调用此函数/方法
我在网上搜索了一个解决方案,特别是在 StackOverflow 上,但我没有找到有用的解决方案。我找到的大多数解决方案都是这两个解决方案之一:
do-while为while。这说明php.net的代码逻辑有问题,我觉得很难相信。但更重要的是,它对我不起作用。while并更改$mysqli->next_result()为$mysqli->next_result() && $mysqli->more_results(),但此解决方案效果不佳。它确实消除了错误,但忽略了最后一个结果。试试看
} while ($mysqli->more_results() && $mysqli->next_result());
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
印刷
'localonly@localhost'
-----------------
'cityU'
'cityV'
'cityW'
'cityX'
'cityY'
Run Code Online (Sandbox Code Playgroud)
没有警告/通知。
| 归档时间: |
|
| 查看次数: |
1282 次 |
| 最近记录: |