本文實(shí)例講述了tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
1.通過(guò)composer安裝
composer require mongodb/mongodb
2.使用
<?php
/**
* @author: jim
* @date: 2017/11/17
*/
namespace app\index\controller;
use think\Controller;
use MongoDB\Driver\Manager;
use MongoDB\Collection;
class MongoTest extends Controller
{
protected $mongoManager;
protected $mongoCollection;
public function __construct()
{
$this->mongoManager = new Manager($this->getUri());
$this->mongoCollection = new Collection($this->mongoManager, "mldn","dept");
}
public function test() {
// 讀取一條數(shù)據(jù)
$data = $this->mongoCollection->findOne();
print_r($data);
}
protected function getUri()
{
return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
}
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!