PHP mysqli准备语句不起作用

2021-12-25 00:00:00 sql php mysqli prepared-statement

我正在使用 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]);

相关文章