gpt4 book ai didi

php - mysqli_stmt_get_result 和 mysqli_fetch_all 不兼容?

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:20 25 4
gpt4 key购买 nike

我试图绑定(bind)参数(为了安全),然后将结果放入一个数组中,所以我创建了如下代码。问题是它不起作用:$arr 为空。 (顺便说一句,我知道 getList 中的查询有效。)

if ($stmt = mysqli_prepare($con, "call getList(?)")) {
mysqli_stmt_bind_param($stmt, 's', $userInputSearch);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$arr = mysqli_fetch_all($result, MYSQLI_ASSOC);
$jsonArr = json_encode($arr);
echo $jsonArr;

请注意,在使用 mysqli_query() 时,我已经让 mysqli_fetch_all 工作。

关于 mysqli_fetch_all引用,我发现了这条评论:

Also, mysqli_fetch_all works only for buffered result sets, which are the default for mysqli_query. MYSQLI_USE_RESULT will be supported in 5.3.4+ However, it makes little sense to use it this way, materialising unbuffered sets. In this case choose STORE_RESULT, and fetch_all won't copy the data, but reference it, as it is stored already in mysqlnd.

我发现准备好的语句返回未缓冲的结果,所以我尝试使用 $result = mysqli_stmt_store_result($stmt) 而不是 $result = mysqli_stmt_get_result($stmt); 但是那没有帮助。

这让我并没有完全不知所措——我知道我可以循环一次加载一行数据,但我真的不想在 PHP 中循环只是为了做一些简单的事情来自准备好的语句的数组。有没有办法将结果集作为一个对象来获取和处理?

最佳答案

我刚刚重读了文档...

我现在不可能测试 mysql。

但只是猜测:

mysqli_fetch_all ( mysqli_result $result ...

Parameters result Procedural style only: A result set identifierreturned by mysqli_query(), mysqli_store_result() ormysqli_use_result().

我想强调所有函数 mysqli_query(), mysqli_store_result() mysqli_use_result() 属于 mysqli 类,而不属于 stmt 并且它们返回类型 mysqli_result

但您正在尝试使用 mysqli_stmt_get_result($stmt);

mysqli_result mysqli_stmt_get_result ( mysqli_stmt $stmt )
...

Return Values
Returns a resultset or FALSE on failure.

所以这个函数返回类型resultset 而不是mysqli_result

因此您必须更改代码以不使用 mysqli_stmt

所以改变:

if ($stmt = mysqli_prepare($con, "call getList(?)")) {
mysqli_stmt_bind_param($stmt, 's', $userInputSearch);
mysqli_stmt_execute($stmt);

$result = mysqli_query ( $con , "call getList(".mysqli_real_escape_string($userInputSearch).")" );

,或只需更改您的线路:

$arr = mysqli_fetch_all($result, MYSQLI_ASSOC);

$arr = array()
while ($row = $result->fetch_array())
{
arr[] = $row ;
}

如有错误请见谅,目前无法跟踪调试,希望对您有所帮助。

关于php - mysqli_stmt_get_result 和 mysqli_fetch_all 不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28630617/

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