0

I keep getting this error when writing database related code. Over the past month or so i've been trying various different methods to get around the errors i'm encountering. The only one i've found being a foreach regarding messaging.

Back story: I have a personal website, to which i am currently trying to incorporate social features like messaging, and friends. In the future i plan to add things like profile pictures and more.

Error:

[TIME] [:error] [pid PID] [client IP:PORT] PHP Fatal error:  Call to a member function bind_param() on a non-object in /www/hub/models/funcs.php on line 1656, referer: http://my.website.com/hub/users.php

The error points to this line:

$stmt->bind_param("i", $user);

which is accessed by this function:

function getUnreadMessages($user)

which is accessed by this resource:

left-nav.php

which is accessed by every page on the site, which is trying to run this line of code:

$unread = getUnreadMessages($loggedInUser->user_id);

So that it can run these lines of code:

if($unread > 0){
    echo "<li><a href='user_messages.php'>Messages</a>(".$unread.")</li>";
}else{
    echo "<li><a href='user_messages.php'>Messages</a></li>";
}

This is the full function:

function getUnreadMessages($user){
    global $mysqli,$db_table_prefix;
    $stmt = $mysqli->prepare("SELECT 
        id,
        from_user,
        to_user,
        time_sent,
        subject,
        message,
        deleted
        FROM ".$db_table_prefix."messages
        WHERE
        to_user = ?
        AND
        read = 0
        ");
    $stmt->bind_param("i", $user);
    $stmt->execute();
    $unread = $stmt->num_rows;
    $stmt->close();
    return($unread);
}

Sorry this is so messy. I hope you can understand my frustration. There are more errors but this is the biggest issue right now. If any of you would be willing to provide more support outside this post, i'd be very greatful.

Not_Lazy
  • 69
  • 1
  • 8

1 Answers1

0

PHP Fatal error: Call to a member function bind_param() on a non-object

Means that the var $stmt is a non object, possibly a NULL meaning as per the PHP error, that it isn't an object on which any method can be called on.

Looking at your getUnreadMessages() function I see that $mysqli is a global. Is it possible some other logic is modifying $mysqli elsewhere in your system?

Do you also see similar PHP Fatal Error's for "Call to a member function prepare() on a non-object" ? If you did, this would point to $mysqli being modified in some way outside of the above logic.

At this point it looks to me though that $stmt is a non-object (var_dump($stmt); what is shown out of interest?) because $mysqli->prepare(...) may not be returning anything (or is returning something, but not an object).

I'd Google for what MySQLi prepare() is supposed to output, if the SQL string is valid and what happens when it isn't.

theruss
  • 1,690
  • 1
  • 12
  • 18
  • `$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);` – Not_Lazy May 20 '16 at 06:20
  • Sorry for late response – Not_Lazy May 20 '16 at 06:21
  • If you can answer my questions one by one, that'd be most useful to myself and anyone else who wishes to respond - namely: What do you get if you `var_dump($stmt));` ? – theruss May 22 '16 at 20:53
  • I've found the answer but I can't add another answer to the post. This helped me(it was the post that was used for flagging this post as a duplicate): http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – Not_Lazy May 24 '16 at 19:00