PHP mysqli prepare statement not working
mysqli, php, prepared-statement, sql
Solution
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]);
Problem
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 Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\wamp\www\firecom\firecom.php on line 80 PHP Notice: Undefined variable: results in C:\wamp\www\firecom\firecom.php on line 89 Both parameters are being set correctly but something is throwing it off. Code: ``` $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: ``` 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) } ```