xampp CodeIgniter上PHP7的MongoDb错误
在使用PHP7在xampp中运行应用程序时,我遇到以下错误。我是mongoDb的新手。无法找出解决此问题的方法。任何关于这个问题的帮助或建议都将不胜感激。谢谢你的帮助。下面是我认为有问题的代码 错误: 遇到未捕获的异常 类型:错误 消息:找不到类“MongoClient” 文件名:C:\xampp\htdocs\Web\application\libraries\Mongo\u db.phpxampp CodeIgniter上PHP7的MongoDb错误,php,mongodb,codeigniter,Php,Mongodb,Codeigniter,在使用PHP7在xampp中运行应用程序时,我遇到以下错误。我是mongoDb的新手。无法找出解决此问题的方法。任何关于这个问题的帮助或建议都将不胜感激。谢谢你的帮助。下面是我认为有问题的代码 错误: 遇到未捕获的异常 类型:错误 消息:找不到类“MongoClient” 文件名:C:\xampp\htdocs\Web\application\libraries\Mongo\u db.php <?php if ( ! defined('BASEPATH')) exit('No direct
<?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);
}
}
}
}
?>
电话号码:49
回溯:
文件:C:\xampp\htdocs\Web\application\controllers\Home.php行:7
函数:_构造
文件:C:\xampp\htdocs\Web\index.php行:315函数:require\u一次
库\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
<?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
<?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
请参阅进一步阅读。一旦安装MongoDB,就无法直接与PHP连接。您需要安装用于PHP的MongoDB驱动程序,可以在以下URL中找到 根据操作系统的类型,您可以下载一个 一旦你做完了。您可以使用composer下载mongo库,以便开始与之交互
{
"require": {
"mongodb/mongodb": "^1.1"
}
}
完成后,您的项目文件夹中将有一个供应商目录,其中包含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;
<?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]
);