php - 重用准备好的语句会产生分段错误
问题描述
我必须编写一个调用存储过程的准备好的语句,并且我需要遍历一个数组,为数组中的每个元素调用该过程。所以我想利用您可以设置语句然后多次运行它的效率,只需将值发送到插入。
但是,当我这样做时,我得到的不是 php 错误,而是浏览器错误:
此页面不工作 localhost 没有发送任何数据。ERR_EMPTY_RESPONSE
apache2 日志显示存在分段错误。
我想做这个:
$sql = $this->conn->prepare("call add_instrument(?, @instrument_id)");
for ($i = 0; $i <= count($new_instrument_names)-1; $i++) {
$sql->bind_param("s", $new_instrument_names[$i]);
$sql->execute();
$sql->store_result();
$sql->bind_result($this->new_instrument_ids[]);
$sql->fetch();
}
$sql->close();
但是上面会产生分段错误。
所以我必须这样做,将准备好的语句放在循环中,以便它发生在 $new_instrument_names 数组中的每个项目上:
for ($i = 0; $i <= count($new_instrument_names)-1; $i++) {
$sql = $this->conn->prepare("call add_instrument(?, @instrument_id)");
$sql->bind_param("s", $new_instrument_names[$i]);
$sql->execute();
$sql->store_result();
$sql->bind_result($this->new_instrument_ids[]);
$sql->fetch();
$sql->close();
}
这可能是php7中的错误还是我的代码有问题?谢谢!
解决方案
尝试这个:
$name = ""; //variable to bind your statement with
$response = ""; // get results
$sql = $this->conn->prepare("call add_instrument(?, @instrument_id)");
$sql->bind_param("s", $name);
for ($i = 0; $i <= count($new_instrument_names)-1; $i++) {
$name = $new_instrument_names[$i];
$sql->execute();
$sql->store_result();
$sql->bind_result($response);
while ($sql->fetch())
array_push($this->new_instrument_ids,$response);
}
$sql->close();
$this->conn->close(); //close the mysqli connection too
推荐阅读
- python - 在 virtualenv 中默认更改 python 版本
- python - 正则表达式 - 后视需要固定宽度的模式错误
- jsf - 单击 h:commandLink 后单击 a4j:commandLink 时页面冻结
- jquery - 如何重置jQuery设置的元素宽度
- python-3.x - 如何使用 Celery 安排在每个月的最后一天运行的任务?
- javascript - 与 new Date().toLocaleDateString() 相比,MAT_DATE_LOCALE 返回不同的输出
- typescript - 键入 assoc 函数以正确推断新类型
- sql - 在 Oracle 中获取 Pivot 的查询
- azure-active-directory - 将用户自定义角色/属性从自定义 Web 应用程序 (CMS)(已与 MS Azure AD 集成)导入 MS Azure AD
- firebase - 如何暂停 Firebase 消息传递(云消息传递)