Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysqli prepare statement not working

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) }
like image 709
Jon Erickson Avatar asked Apr 02 '13 17:04

Jon Erickson


1 Answers

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]);
like image 139
Your Common Sense Avatar answered Sep 19 '22 23:09

Your Common Sense