doudiewen9435 2016-05-20 02:56
浏览 38

PHP和MySQL我看不到这个错误? [重复]

This question already has an answer here:

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.

</div>
  • 写回答

1条回答 默认 最新

  • dougai3418 2016-05-20 03:12
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)