PHP mysqli准备语句不起作用

时间:2023-03-06
本文介绍了PHP mysqli准备语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 mysqli prepare 语句来查询具有多个约束的数据库.我已经在我的测试文件中运行了代码,它运行良好.但是,当我将代码移至实时文件时,它会引发以下错误:

I am using a mysqli prepare statement to query my db with multiple constraints. I have ran the code in a test file of mine and it works perfectly fine. However, when I move the code over to my live file it throws the error below:

PHP 警告:mysqli_stmt::bind_result():绑定变量的数量与准备好的语句中的字段数不匹配C:wampwwwfirecomfirecom.php 80 行

PHP Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:wampwwwfirecomfirecom.php on line 80

PHP 注意:未定义变量:导致C:wampwwwfirecomfirecom.php 第 89 行

PHP Notice: Undefined variable: results in C:wampwwwfirecomfirecom.php on line 89

两个参数都设置正确,但有些东西把它扔掉了.

Both parameters are being set correctly but something is throwing it off.

代码:

$query = $mysqli->prepare("SELECT * FROM calls WHERE wcccanumber = ? && county = ?");
$query->bind_param("ss", $wcccanumber, $county);
$query->execute();

$meta = $query->result_metadata();

while ($field = $meta->fetch_field()) {
    $parameters[] = &$row[$field->name];
}

call_user_func_array(array($query, 'bind_result'), $parameters);

while ($query->fetch()) {
    foreach($row as $key => $val) {
        $x[$key] = $val;
    }
    $results[] = $x;
}

print_r($results['0']);

$query var_dump:

$query var_dump:

object(mysqli_stmt)#27 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(2) ["field_count"]=> int(13) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

推荐答案

为什么要用 mysqli 折磨自己?
在 PDO 中,您不需要这些可怕的代码,只需要一行代码即可获得结果

Why torture yourself with mysqli?
In PDO you will need none of these horrendous codes, but only one line to get the results

$query = $pdo->prepare("SELECT * FROM calls WHERE wcccanumber = ? && county = ?");
$query->execute(array($wcccanumber, $county));
$results = $query->fetchAll();
print_r($results[0]);

这篇关于PHP mysqli准备语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:是否可以将 store_result() 和 bind_result() 与 PHP PDO 一起使用? 下一篇:调用非对象上的成员函数 bind_param()(尽管经过研究仍无法解决)

相关文章