Eleeist
Eleeist

Reputation: 7041

Error when returning prepared statement result

I have the following function:

class Forums {
        public function getForum($id) {
            $database = new Database();
            $mysqli = $database->databaseConnection();
            $stmt = $mysqli->prepare("SELECT name, description FROM forums WHERE id = ?");
            $stmt->bind_param("i", $id);
            $stmt->execute();
            return $stmt;
        }
}

I invoke it like this:

$forums = new Forums();
$result = $forums->getForum($_GET["id"]);
$result->bind_result($name, $description);
$result->fetch();

And then, since it returns only one row, call $name or $description whenever I need it.

However I get the following error in my browser (Chrome):

Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data."

This does not happen if I get rid of the function and connect to database and create prepared statement directly in the file where the result is used.

Why is this happening?

Upvotes: 1

Views: 91

Answers (1)

Valera Leontyev
Valera Leontyev

Reputation: 1181

Looks like a variable scope issue. Object that holds $mysqli is destroyed directly after getForum() method execution. So open DB connection is destroyed at this moment too. But $result->fetch(); need to be connection still open.

Upvotes: 2

Related Questions