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!

4
  • What is your input into the function? Commented Aug 12, 2013 at 19:34
  • @LiamSorsby $class->__verify(4992,1920) Commented Aug 12, 2013 at 19:35
  • Shouldn't you have multiple binds? One with each parameter? Commented Aug 12, 2013 at 19:36
  • @LiamSorsby Negative. Tried it out & it duplicated the error. Commented Aug 12, 2013 at 19:39

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

it does. in the form of get_result.
@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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.