PHP 中使用 MongoDB

Installation and setting

  • Installation mongo.so, run as follow:

     git clone https://github.com/mongodb/mongo-php-driver
     cd mongo-php-driver
     phpize 
     ./configure
     make
     sudo make install
    
  • Then add

      extension=mongo.so
    

    to your php.ini file.

note:if you can‘t find any php.ini in your computer, try to search php.ini.default, and rename it to php.ini.

  sudo cp /etc/php.ini.default /etc/php.ini
  • Tips

(1) when you change the php.ini file, should restart apache

    sudo apachectl restart

(2) You can check the php setting by this:

    <?php phpinfo(); ?>

Use mongodb in codeigniter

  • fetch codeigniter-mongodb-library

      git clone https://github.com/cp0000/codeigniter-mongodb-library.git
    
  • Then copy the mongodb.php into cofig folder, copy Mongo_db.php into the libraries

  • Modify autoload.php, make sure load the mongoddb.

      $autoload[‘libraries‘] = array(‘mongo_db‘, ‘session‘, ‘user_agent‘);
    
  • Now you can use the mongodb in the codeigniter project;

      public function index()
      {
           $this->mongo_db->insert (‘User‘, array (
                            ‘user_name‘    => ‘chengpei‘,
                            ‘create_time‘  => date (‘Y-m-d H:i:s‘)
                            ));
      }

PHP 中使用 MongoDB,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。