-1

Lets say I have a prepared statement. The query that it prepares doesn't matter. I fetch the result like above (I can't show actual code, as it is something I don't want to show off. Please concentrate on the problem, not the examples meaningless) and I get

Fatal error: Call to a member function bind_param() on a non-object in... error. The error caused in the called object.

<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

class table2Info{
    private $mysqli;

    public function __construct($_mysqli){
        $this->mysqli = $_mysqli;
    }

    public function getInfo($id)
    {
        $db = $this->mysqli->prepare('SELECT info FROM table2 WHERE id = ? LIMIT 1');
        $db->bind_param('i',$db_id);

        $db_id  = $id;

        $db->bind_result($info);
        $db->execute();
        $db->fetch();

        $db->close();

        return $info;
    }

}
$t2I = new table2Info($mysqli);

$stmt->prepare('SELECT id FROM table1 WHERE name = ?');
$stmt->bind_param('s',$name);

$name = $_GET['name'];

$stmt->bind_result($id);
$stmt->execute();

while($stmt->fetch())
{
    //This will cause the fatal-error
    echo $t2I->getInfo($id); 
}
$stmt->close();
?>

The question is: is there a way to do another prepared statement while another one is still open? It would simplify the code for me. I can't solve this with SQL JOIN or something like that, it must be this way. Now I collect the fetched data in an array and loop through it after $stmt->close(); but that just isn't good solution. Why should I do two loops when one is better?

hakre
  • 193,403
  • 52
  • 435
  • 836
Ákos Nikházy
  • 1,276
  • 17
  • 33
  • 1
    [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) – hakre Dec 15 '12 at 20:13
  • possible duplicate of [mysqli bind\_param() fatal error](http://stackoverflow.com/questions/6582512/mysqli-bind-param-fatal-error) – Jocelyn Dec 16 '12 at 00:32

1 Answers1

0

From the error you're getting it appears that your statement preparation failed. mysqli::prepare returns a MySQLi_STMT object or false on failure.

Check for the return value from your statement preparation that is causing the error. If it is false you can see more details by looking at mysqli::error.

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
  • It fails only when it is before the $stmt->close(). The problem is I don't see a way to do another prepared statement while other is open. The bind_param() inside the table2Info object trying to use the already open statement instead the one inside the object. – Ákos Nikházy Sep 11 '11 at 13:59