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.