dtpwra8456 2013-11-16 03:31
浏览 21
已采纳

使用实例变量[duplicate]调用非对象的成员函数

This question already has an answer here:

I have the below code which, when run, returns this error:

PHP Fatal error: Call to a member function count() on a non-object in /classes/User.php on line 36

public function find($user = null) {
    if($user) {
        $field = (is_numeric($user)) ? 'id' : 'email';
        $data = $this->_db->get('*', 'users', array($field, '=', $user));
        if($data->count()) { // This is line 36, the $_db variable is an instance of the DB class
            $this->_data = $data->first();
            return true;
        }
    }
    return false;
}

What am I missing that is making $data not refer to an object?

</div>
  • 写回答

1条回答 默认 最新

  • droc60607 2013-11-16 03:37
    关注

    It's pretty simple, though the solution may not be: $this->_db->get is not returning an object. Depending on what _db is - is it an abstraction layer? Some crazy homebrew thing? Just looking at the syntax:

    $this->_db->get('*', 'users', array($field, '=', $user));
    

    I get a little weirded out - assuming that it's describing how keys are separated from search values, I've never seen a DB layer like that (and how often does SQL change what = does, anyway?) I'm willing to bet your database layer needs some attention, but that might be editorializing a bit. ;)

    Chances are whatever it is, it's returning an error - it's (unfortunately) fairly commonplace for an object library to return FALSE instead of an object with an error code. It could even be returning an error string. Echo that bad boy and see what you get, and then fix whatever's causing it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?