如何在codeigniter中集成mongodb库以与mongodb建立数据库连接 [英] how to integrate mongodb library in codeigniter for database connection with mongodb

查看:137
本文介绍了如何在codeigniter中集成mongodb库以与mongodb建立数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeigniter和mongodb数据库创建应用程序。由于codeigniter不包含mongodbs驱动程序,因此我使用了内置库来连接mongodb。我已使用此链接来创建mongodb配置文件和Mongo_db库。
配置文件类似于application isconfig\mongodb:

I am creating application with codeigniter and mongodb database. As codeigniter doesn't contain mongodbs driver, I have used built in library to connect with mongodb. I have used this link to create mongodb configration file and Mongo_db library. The configration file is like application\config\mongodb:

<?php
$config['default']['mongo_hostbase'] = 'localhost:27017';
$config['default']['mongo_database'] = 'test';
$config['default']['mongo_username'] = '';
$config['default']['mongo_password'] = '';
$config['default']['mongo_persist']  = FALSE;
$config['default']['mongo_persist_key']  = 'ci_persist';
$config['default']['mongo_replica_set']  = FALSE;
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = FALSE;
$config['default']['mongo_host_db_flag']   = FALSE; 

?> 

和Mongo_db库就像:

and Mongo_db library is like:

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

class Mongo_db
{
  public function __construct()
    {

                //Check mongodb is installed in your server otherwise display an error
        if ( ! class_exists('Mongo'))
        {
            $this->_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 Mongo('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);           
        }


}
?>

当我运行应用程序时,它给我一个像
的错误遇到了
您的application / config / mongodb.php文件似乎不包含有效的配置数组。

When I run the application it give me an error like An Error Was Encountered Your application/config/mongodb.php file does not appear to contain a valid configuration array.

有人可以告诉如何解决此错误并建立与mongodb的连接吗?

can anybody tell How to resolve this error and setup connection with mongodb?

推荐答案

您可能想尝试此回购协议: https://github.com/vesparny/cimongo-codeigniter-mongodb-library ,似乎非常易于使用。

You might want to try this repo: https://github.com/vesparny/cimongo-codeigniter-mongodb-library, seems pretty easy to use.

完整的PHP库列表可以在这里找到:
http://docs.mongodb.org/ecosystem/drivers/php-libraries/

A full list of PHP libraries can be found here: http://docs.mongodb.org/ecosystem/drivers/php-libraries/

这篇关于如何在codeigniter中集成mongodb库以与mongodb建立数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆