gpt4 book ai didi

PHP:foreach msqli_query 问题

转载 作者:行者123 更新时间:2023-11-29 23:27:58 25 4
gpt4 key购买 nike

我现在遇到了一小段代码的奇怪问题。我花了太多时间试图弄清楚,所以我想我会在这里问。我有一个整数数组 ($childIDs),我想用它来单独调用 MySQL 数据库中的存储过程。连接设置得很好,而且这个结构到目前为止还没有给我带来任何问题。

$childIDs 数组已正确设置,并且 foreach 循环确实将数组中的每个整数作为 $currentChild 进行循环。我首先注意到只有数组中的第一项会显示。经过一些测试,我发现在第一次循环迭代后 $result 被设置为 bool(false) 。话虽这么说,查询与我在数组中使用的数字配合得很好。

所以我的问题是为什么 $result = mysqli_query($database, "CALL get_notes($currentChild);") 在除了 foreach 循环的第一次迭代之外的所有内容上都是 false bool?

代码如下:

$childIDs = array();
$childIDs = json_decode($_GET['childids']);
$noteList = array();

foreach ($childIDs as $currentChild)
{
if ($result = mysqli_query($database, "CALL get_notes($currentChild);"))
{
// Gathers all the notes for the child
while($row = mysqli_fetch_array($result))
{
// does stuff with each row, for now I'll just use an example...
var_dump($row);
}
}
}

最佳答案

这是通过mysqli_next_result()解决的。我需要在每次调用 mysql_query() 的新迭代之前释放 mysqli。这是工作代码:

$childIDs = array();
$childIDs = json_decode($_GET['childids']);
$noteList = array();

foreach ($childIDs as $currentChild)
{
if ($result = mysqli_query($database, "CALL get_notes($currentChild);"))
{
// Gathers all the notes for the child
while($row = mysqli_fetch_array($result))
{
// does stuff with each row, for now I'll just use an example...
var_dump($row);
}
}

mysqli_next_result($database);
}

关于PHP:foreach msqli_query 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26805290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com