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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line