1

I'm having an issue trying to bind my result. PHP keeps outputting an error with the following

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in /var/www/public_html/test.php on line 38

Now before anyone starts referencing other links, I have already looked up and down stackoverflow and done a little searching on Google as well. All the examples I've found, including PHP.net, say this is correct... But evidently it's not.

Here is what I have:

function __verify($digit4, $woid) {
    $query = $this->mysql->prepare("SELECT * FROM pc_wo wo LEFT JOIN pc_owner owner ON owner.pcid=wo.pcid WHERE wo.woid=? AND SUBSTRING(owner.pcphone, -4)=?");
    $query->bind_param("is",$woid,$digit4);
    if ( !$query->execute() ) return false;
    $query->bind_result($resp);
    $query->fetch();

    var_dump($resp);
    return true;
}

EDIT I suppose you can't use bind_result for a wildcard select (*)... So what do I use in accordance with mysqli_stmt to fetch an entire array?

Thank you!

user0000001
  • 2,092
  • 2
  • 20
  • 48

2 Answers2

1

My solution was using the pseudo method:

private function __compile($handler)
{
    $meta = $handler->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = &$row[$field->name];
    }

    call_user_func_array(array($handler, 'bind_result'), $params);

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

    return $results;
}

This might be a solution, but why does MySQLi not incorporate a prepared statement for wildcard statements?

user0000001
  • 2,092
  • 2
  • 20
  • 48
  • it does. in the form of get_result. – Your Common Sense Aug 12 '13 at 20:12
  • Use PDO then. See http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons?rq=1 – huysentruitw Aug 12 '13 at 20:32
  • @WouterHuysentruit Great suggestion. I jumped on board to MySQLi because I am just parting away from the old PHP MySQL extension and thought it would be familiar... But after looking at PDO, the OO aspect of things is very interesting :o... Thanks for this share. – user0000001 Aug 13 '13 at 16:31
0

if you get lucky enough, you can use get_result():

if ( !$query->execute() ) return false;
$res = $query->get_result();
$row = $res->fetch-assoc();

if not - you are bound to use these get_metadata() tricks from other answer.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345