PHP封裝的簡(jiǎn)單連接MongoDB類示例
本文實(shí)例講述了PHP封裝的簡(jiǎn)單連接MongoDB類。分享給大家供大家參考,具體如下:
1. 封裝MongoDB類
<?php class MongoDB { private $database; private $mongo; function __construct() { $this->mongo = new MongoClient("mongodb://user:password@server_address:port/admin"); $this->database = $this->mongo->selectDB("data"); } { return $this->database->selectCollection($collection); } //獲取所有的集合名 function getCollections() { return $this->database->getCollectionNames(); } //選數(shù)據(jù)庫(kù) function selectDB($db) { $this->database = $this->mongo->selectDB($db); } }
2. 簡(jiǎn)單調(diào)用,insert數(shù)據(jù)。
class DemoController extends CI_Controller { function __construct() { parent::__construct(); //CI中加載類 $this->load->library('mongo_lib', '', 'mongodb'); } //插入一條數(shù)據(jù) function create() { $data = array('name'=>'mike','email'=>'abc@163.com); //選擇庫(kù),shell:user demo_db $this->mongodb->selectDB('demo_db'); //選擇集合,db.demo_col.insert(); $rebateCollection = $this->mongodb->getCollection('demo_collection'); $res = $rebateCollection->insert($data); } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP函數(shù)import_request_variables()用法分析
這篇文章主要介紹了PHP函數(shù)import_request_variables()用法,結(jié)合實(shí)例形式分析了import_request_variables函數(shù)的功能,定義及相關(guān)使用技巧,需要的朋友可以參考下2016-04-04深入解析WordPress中加載模板的get_template_part函數(shù)
這篇文章主要介紹了WordPress中加載模板的get_template_part函數(shù),其中重點(diǎn)講解了其函數(shù)鉤子的使用,需要的朋友可以參考下2016-01-01php中隱形字符65279(utf-8的BOM頭)問(wèn)題
這篇文章主要介紹了php中隱形字符65279問(wèn)題解決方法,其實(shí)就是utf-8文件的BOM頭,本文給出了一個(gè)刪除PHP文件BOM頭的小程序,需要的朋友可以參考下2014-08-08PHP 函數(shù)學(xué)習(xí)簡(jiǎn)單小結(jié)
下面是一些php下經(jīng)常用的函數(shù),都是些必須要知道的函數(shù),只有知道有個(gè)函數(shù)與功能,才可能組裝成完整的功能強(qiáng)大的系統(tǒng)。2010-07-07