Skip to content
Advertisement

Fatal error stating “Call to a member function bind_param() on a non-object”

I’ve gotten a fatal error, which I do not know where went wrong.

My Category page,

<?php 
require_once 'dbfunction.php'; 
require_once 'DBCategory.php';

$con=getDbConnect();
$categoryArr=getcategoryArrCon($con, STATUS_ACTIVE); 

foreach ($categoryArr as $categoryName=>$CategoryInfoArr) { 

?>

and this is my function script for getCategoryArrCon

<?php

define("STATUS_ACTIVE", 0);
define("STATUS_DELETE", 1);

function getcategoryArrCon($con, $status) {
    $result = array();
    if (!$con->connect_error) {
        $sqlstr = "SELECT c.category, c.title, c.image" .
                "FROM category c WHERE status=?";
        $stmt = $con->prepare($sqlstr);
        $stmt->bind_param("i", $status);
        $stmt->excute();
        $stmt->bind_result($category, $title, $image);
        while ($stmt->fetch()) {
            $result[$category] = [
                        "category" => $category,
                        "title" => $title,
                        "image" => $image
            ];
        }
        $stmt ->close();
    }
    return $result;
}
?>

Advertisement

Answer

If you have noticed, when you echo your prepared statement, it will look just like this:

SELECT c.category, c.title, c.imageFROM category c WHERE status=?

That simple space is important also in the middle of c.image and FROM to make the query valid.

$sqlstr = "SELECT c.category, c.title, c.image" .
                " FROM category c WHERE status=?";
             //  ^

Or why not just make it a one liner:

$sqlstr = "SELECT c.category, c.title, c.image FROM category c WHERE status = ?";

And also make sure that $stmt->excute(); isn’t really a typo. Its ->execute()

Sidenote: Also, make use of ->error to check whether the preparation is okay. Example:

$stmt = $con->prepare($sqlstr) or die($con->error);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement