【问题标题】:MongoDb error with php 7 on xampp CodeIgniterxampp CodeIgniter 上的 php 7 出现 MongoDb 错误
【发布时间】:2017-09-19 05:32:57
【问题描述】:

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

错误:

遇到未捕获的异常

类型:错误

消息:找不到类“MongoClient”

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

行号:49

回溯:

文件:C:\xampp\htdocs\Web\application\controllers\Home.php 行:7 函数:__构造

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

库\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);
            }
        }
    }



}
?>

配置/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';
?>

配置/mongo.php

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

【问题讨论】:

    标签: php mongodb codeigniter


    【解决方案1】:

    MongoCLient 类由 pecl install mongo 提供。但是 pecl/mongo 不适用于 php7 并且不推荐使用以支持 pecl/mongodb。但是使用 pecl/mongodb 你需要使用 MongoDB\Driver\Manager 而不是 MongoClient

    请参阅here 以进一步阅读。

    【讨论】:

      【解决方案2】:

      一旦安装了 MongoDB,就无法直接连接 PHP。您需要安装适用于 PHP 的 MongoDB 驱动程序,您可以在以下 URL 中找到该驱动程序

      http://php.net/manual/en/mongodb.installation.php

      根据您可以下载的操作系统类型。

      一旦你完成了。您可以使用 composer 下载 mongo 库,以便开始与它进行交互

      {
          "require": {
              "mongodb/mongodb": "^1.1"
          }
      }
      

      完成后,您的项目文件夹中将有一个 vendor 目录,其中包含 autoload.php 将处理自动加载所需的依赖库和所需的依赖库在供应商中。

      您可以按如下方式开始使用该库

      db_connect.php

      <?php
      /* Require the Vendor for autoloading MongoDb Client */
      include_once 'vendor/autoload.php';
      
      /* Create the Object of Mongodb */
      $mongoDB                = new MongoDB\Client;
      
      /* Creating the database on which I will be working */
      $erpDB          = $mongoDB->database_name;
      

      上面的代码可以如下使用

      products.php

      <?php
      
      include_once 'DB/db_connect.php';
      /* The following creates the products collection */
      $productsCollection     = $erpDB->products;
      
      $insertedData       = $productsCollection->insertOne(
          ['_id' => $idCounter, 'product_name' => $productName, 'product_quantity' => $productQuantity]
      );
      

      【讨论】:

        猜你喜欢
        • 2015-03-22
        • 1970-01-01
        • 1970-01-01
        • 2014-08-26
        • 2018-07-09
        • 2012-08-15
        • 2014-03-21
        • 1970-01-01
        • 2017-02-13
        相关资源
        最近更新 更多