致命错误:在C:\ xampp \ htdocs \ phpmongodb \ db.php中的非对象上调用成员函数selectCollection()

时间:2015-11-11 16:02:15

标签: php fatal-error

有人可以帮助我,当我尝试在浏览器中运行代码时,会收到此致命错误消息。我搜索了各种主题,但无法找到答案。我有两个非常相似的功能,其中只有一个报告错误。函数getById工作正常,但函数get报告错误。这是我的db.php文件:

class DB {

    private $db;

    public $limit = 5;

    public function __construct($config){

        $this->connect($config);    
    }

    private function connect($config){
        try{
        if ( !class_exists('Mongo')){
                echo ("The MongoDB PECL extension has not been installed or enabled");
                return false;
            }
            $connection=  new MongoClient($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password']));
            return $this->db = $connection->selectDB($config['dbname']);
        }catch(Exception $e) {
            return false;
        }
    }

    public function create($collection,$article){

        $table   = $this->db->selectCollection($collection);
        return $result = $table->insert($article);
    }

    public function get($page,$collection){

        $currentPage = $page;
        $articlesPerPage = $this->limit;

        //number of article to skip from beginning
        $skip = ($currentPage - 1) * $articlesPerPage; 

        $table = $this->db->selectCollection($collection); **//here reports an error**

        $cursor = $table->find();
        //total number of articles in database
        $totalArticles = $cursor->count(); 
        //total number of pages to display
        $totalPages = (int) ceil($totalArticles / $articlesPerPage); 

        $cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesPerPage);
        //$cursor = iterator_to_array($cursor);
        $data=array($currentPage,$totalPages,$cursor);

        return $data;
    }

    public function getById($id,$collection){
        // Convert strings of right length to MongoID
        if (strlen($id) == 24){
            $id = new MongoId($id);
        }
        $table = $this->db->selectCollection($collection);
        $cursor  = $table->find(array('_id' => $id));
        $article = $cursor->getNext();

        if (!$article ){
            return false ;
        }
        return $article;
    }

    public function delete($id,$collection){
        // Convert strings of right length to MongoID
        if (strlen($id) == 24){
            $id = new \MongoId($id);
        }
        $table   = $this->db->selectCollection($collection);
        $result = $table->remove(array('_id'=>$id));
        if (!$id){
            return false;
        }
        return $result;

    }

    public function update($id,$collection,$article){
        // Convert strings of right length to MongoID
        if (strlen($id) == 24){
            $id = new \MongoId($id);
        }
        $table   = $this->db->selectCollection($collection);
        $result  = $table->update(
                array('_id' => new \MongoId($id)), 
                array('$set' => $article)
        );
        if (!$id){
        return false;
        }
        return $result;

    }

    public function commentId($id,$collection,$comment){

        $postCollection = $this->db->selectCollection($collection);
        $post = $postCollection->findOne(array('_id' => new \MongoId($id)));

        if (isset($post['comments'])) {
            $comments = $post['comments'];
        }else{
            $comments = array();
        }                   
        array_push($comments, $comment);

        return $postCollection->update(
                    array('_id' => new \MongoId($id)), 
                    array('$set' => array('comments' => $comments))
        );
    }

}

0 个答案:

没有答案