PHP mysqli准备语句不起作用 [英] PHP mysqli prepare statement not working

查看:22
本文介绍了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准备语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆