php - 缺少存储过程的第一个结果
问题描述
我执行存储过程的查询,其中有 3 个选择结果
select _id a;
select 2 a;
select 3 a;
我有这样的mysqli来调用存储过程
$i = 0;
if ($server->connection->query("CALL sp_select(1)", MYSQLI_USE_RESULT)) {
printf("res\n");
do {
$i++;
printf("$i.do\n");
if ($result = $server->connection->store_result()) {
printf("$i.store\n");
$data = $result->fetch_all(MYSQLI_ASSOC);
$this->data[] = $data;
$result->free_result();
printf("$i.free\n");
}
if ($server->connection->more_results()) {
printf("$i.more\n");
}
} while ($server->connection->next_result());
echo json_encode($this->data);
}
该函数无法存储第一个结果。我只得到了第二和第三个结果。为什么我无法存储第一个结果?
解决方案
调用存储过程时mysqli_query()
,函数本身会返回第一个结果。像这样的东西也应该收集第一个结果。
$result = $mysqli->query("CALL sp_select()");
$this->data[] = $result->fetch_all();
while ($mysqli->next_result()) {
if ($result = $mysqli->store_result()) {
$this->data[] = $result->fetch_all();
}
}
echo json_encode($this->data);
看起来您混淆了query()
whichmulti_query()
不会返回初始结果。此代码将执行相同的操作:
$mysqli->multi_query("CALL sp_select()");
do {
if ($result = $mysqli->store_result()) {
$this->data[] = $result->fetch_all();
}
} while ($mysqli->next_result());
echo json_encode($this->data);
推荐阅读
- python-3.x - python中lambda函数的参考问题
- react-native - 是否可以在 appcenter 构建中运行排毒测试
- laravel-5 - 在android java中使用laravel rest api处理fcm显示消息和数据消息
- python - 将值分配给字典python中的键
- mysql - MySQL查询从12小时时间格式中选择最近的时间
- google-apps-script - 不允许的操作
- node.js - 无法在nodejs中使用office365帐户从nodemailer发送电子邮件,出现错误身份验证不成功?
- javascript - 如何使用chartJS在x轴上绘制范围标签
- arduino - ESP32 - SoftwareSerial Livrary
- webhooks - 在 MS 团队消息卡的表中添加数据