本文介绍了Xampp CodeIgniter上的php 7出现MongoDb错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有Php 7的xampp中运行该应用程序时出现以下错误.我是mongoDb的新手.无法弄清楚什么可以解决此问题.关于这个问题的任何帮助或建议,将不胜感激.谢谢您的帮助.以下是我认为有问题的代码.

I am getting the following error when running the app in xampp with Php 7. I am new to mongoDb. Can't figure out what might solve this issue. Any help or suggestion about the problem will be highly appreciated. Thank you for your help. Below is the code I believe have some issue.

错误:

类型:错误

消息:找不到类'MongoClient'

Message: Class 'MongoClient' not found

文件名:C:\ xampp \ htdocs \ Web \ application \ libraries \ Mongo_db.php

Filename: C:\xampp\htdocs\Web\application\libraries\Mongo_db.php

行号:49

回溯:

文件:C:\ xampp \ htdocs \ Web \ application \ controllers \ Home.php行:7 功能:__ construct

File: C:\xampp\htdocs\Web\application\controllers\Home.php Line: 7 Function: __construct

文件:C:\ xampp \ htdocs \ Web \ index.php行:315功能:require_once

File: C:\xampp\htdocs\Web\index.php Line: 315 Function: require_once

libraries \ Mongo_db.php

libraries\Mongo_db.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mongo_db
{

        private $debug;
    private $write_concerns;
    private $journal;
    private $selects = array();
    private $updates = array();
    private $wheres = array();
    private $limit  = 999999;
    private $offset = 0;
    private $sorts  = array();
    private $return_as = 'array';
    public $benchmark = array();
    public function __construct()
    {

    //Check mongodb is installed in your server otherwise display an error
    /*if ( ! class_exists('Mongo') && ! class_exists('MongoClient'))
        {
            show_error("The MongoDB PECL extension has not been installed or enabled", 500);
        }*/
    if (!class_exists('MongoDB\Driver\Manager')) {
   show_error("The MongoDB PECL extension has not been installed or enabled", 500);
}

            //get instance of CI class
            if (function_exists('get_instance'))
            {
            $this->_ci = get_instance();
            }

            else
            {
            $this->_ci = NULL;
            }

//load the config file which we have created in 'config' directory
$this->_ci->load->config('mongodb');

$config='default';
// Fetch Mongo server and database configuration from config file which we have created in 'config' directory
$config_data = $this->_ci->config->item($config);

try{
//connect to the mongodb server
$this->mb = new MongoClient('mongodb://'.$config_data['mongo_hostbase']);

//select the mongodb database

$this->db=$this->mb->selectDB($config_data['mongo_database']);

}
catch (MongoConnectionException $exception)
{
//if mongodb is not connect, then display the error
show_error('Unable to connect to Database', 500);
}

}


/**
    * --------------------------------------------------------------------------------
    * Aggregation Operation
    * --------------------------------------------------------------------------------
    *
    * Perform aggregation on mongodb collection
    *
    * @usage : $this->mongo_db->aggregate('foo', $ops = array());
    */
    public function aggregate($collection, $operation)
    {
        if (empty($collection))
        {
            show_error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
        }

        if (empty($operation) && !is_array($operation))
        {
            show_error("Operation must be an array to perform aggregate.", 500);
        }

        try
        {
            $documents = $this->db->{$collection}->aggregate($operation);
            //$this->_clear();

            if ($this->return_as == 'object')
            {
                return (object)$documents;
            }
            else
            {
                return $documents;
            }
        }
        catch (MongoResultException $e)
        {

            if(isset($this->debug) == TRUE && $this->debug == TRUE)
            {
                show_error("Aggregation operation failed: {$e->getMessage()}", 500);
            }
            else
            {
                show_error("Aggregation operation failed: {$e->getMessage()}", 500);
            }
        }
    }



}
?>

config/mongodb.php

config/mongodb.php

<?php

//mongodb host
$config['default']['mongo_hostbase'] = 'localhost';
//mongodb name

$config['default']['mongo_database'] = 'appname';
//mongodb username - by default, it is empty
$config['default']['mongo_username'] = 'root';
//mongodb password - by default, it is empty
$config['default']['mongo_password'] = 'root';
?>

config/mongo.php

config/mongo.php

<?php
$config['mongo_server'] = 'localhost:27017';
$config['mongo_dbname'] = 'appname';
?>

推荐答案

MongoCLient类由pecl install mongo提供.但是pecl/mongo不适用于php7,已弃用pecl/mongodb.但是使用pecl/mongodb时,您将需要使用MongoDB \ Driver \ Manager而不是MongoClient

The MongoCLient class was provided by pecl install mongo. But pecl/mongo is not available for php7 and deprecated in favor of pecl/mongodb. But with pecl/mongodb you'll need to use MongoDB\Driver\Manager instead of MongoClient

请进一步阅读​​此处.

这篇关于Xampp CodeIgniter上的php 7出现MongoDb错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:26