亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php實現(xiàn)的mongodb操作類

 更新時間:2015年05月28日 11:39:15   投稿:hebedich  
說到php連mongoDB,不得不先介紹一下php的官方手冊,網(wǎng)址在:http://us.php.net/manual/en/book.mongo.php,接下來給大家分享一個本人常用的MONGODB的操作類,詳見的數(shù)據(jù)庫操作都有了,小伙伴可以參考下。

mongo_db.php

<?php
 
/**
 * Created by PhpStorm.
 * User: yangyulong
 * Date: 2015/5/26
 * Time: 13:45
 */
class Mongo_db
{
  private static $instanceof = NULL;
  public $mongo;
  private $host = 'localhost';
  private $port = '27017';
 
  private $db;
  public $dbname;
  private $table = NULL;
 
  /**
   * 初始化類,得到mongo的實例對象
   */
  public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL)
  {
 
    if (NULL === $dbname) {
      $this->throwError('集合不能為空!');
    }
 
    //判斷是否傳遞了host和port
    if (NULL !== $host) {
      $this->host = $host;
    }
 
    if (NULL !== $port) {
      $this->port = $port;
    }
 
    $this->table = $table;
 
    $this->mongo = new MongoClient($this->host . ':' . $this->port);
    if ($this->getVersion() >= '0.9.0') {
      $this->dbname = $this->mongo->selectDB($dbname);
      $this->db = $this->dbname->selectCollection($table);
    } else {
      $this->db = $this->mongo->$dbname->$table;
    }
  }
 
  public function getVersion()
  {
    return MongoClient::VERSION;
  }
 
  /**
   * 單例模式
   * @return Mongo|null
   */
  //public static function getInstance($host=null, $port=null, $dbname=null, $table=null){
  //
  //  if(!(self::$instanceof instanceof self)){
  //    self::$instanceof = new self($host, $port, $dbname, $table);
  //  }
  //
  //  return self::$instanceof;
  //}
 
  /**
   * 插入一條數(shù)據(jù)
   * @param array $doc
   */
  public function insert($doc = array())
  {
    if (empty($doc)) {
      $this->throwError('插入的數(shù)據(jù)不能為空!');
    }
    //保存數(shù)據(jù)信息
    try {
      if (!$this->db->insert($doc)) {
        throw new MongoException('插入數(shù)據(jù)失敗');
      }
    } catch (MongoException $e) {
      $this->throwError($e->getMessage());
    }
  }
 
  /**
   * 插入多條數(shù)據(jù)信息
   * @param array $doc
   */
  public function insertMulti($doc = array())
  {
    if (empty($doc)) {
      $this->throwError('插入的數(shù)據(jù)不能為空!');
    }
    //插入數(shù)據(jù)信息
    foreach ($doc as $key => $val) {
      //判斷$val是不是數(shù)組
      if (is_array($val)) {
        $this->insert($val);
      }
    }
  }
 
  /**
   * 查找一條記錄
   * @return array|null
   */
  public function findOne($where = NULL)
  {
    if (NULL === $where) {
      try {
        if ($result = $this->db->findOne()) {
          return $result;
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    } else {
      try {
        if ($result = $this->db->findOne($where)) {
          return $result;
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    }
 
  }
 
  /**
   * todo 帶條件的隨后做
   * 查找所有的文檔
   * @return MongoCursor
   */
  public function find($where = NULL)
  {
    if (NULL === $where) {
 
      try {
        if ($result = $this->db->find()) {
 
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    } else {
      try {
        if ($result = $this->db->find($where)) {
 
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    }
 
    $arr = array();
    foreach ($result as $id => $val) {
      $arr[] = $val;
    }
 
    return $arr;
  }
 
  /**
   * 獲取記錄條數(shù)
   * @return int
   */
  public function getCount()
  {
    try {
      if ($count = $this->db->count()) {
        return $count;
      } else {
        throw new MongoException('查找總數(shù)失敗');
      }
    } catch (MongoException $e) {
      $this->throwError($e->getMessage());
    }
  }
 
  /**
   * 獲取所有的數(shù)據(jù)庫
   * @return array
   */
  public function getDbs()
  {
    return $this->mongo->listDBs();
  }
 
  /**
   * 刪除數(shù)據(jù)庫
   * @param null $dbname
   * @return mixed
   */
  public function dropDb($dbname = NULL)
  {
    if (NULL !== $dbname) {
      $retult = $this->mongo->dropDB($dbname);
      if ($retult['ok']) {
        return TRUE;
      } else {
        return FALSE;
      }
    }
    $this->throwError('請輸入要刪除的數(shù)據(jù)庫名稱');
  }
 
  /**
   * 強制關閉數(shù)據(jù)庫的鏈接
   */
  public function closeDb()
  {
    $this->mongo->close(TRUE);
  }
 
  /**
   * 輸出錯誤信息
   * @param $errorInfo 錯誤內(nèi)容
   */
  public function throwError($errorInfo='')
  {
    echo "<h3>出錯了:$errorInfo</h3>";
    die();
  }
 
}

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關文章

  • 微信公眾號OAuth2.0網(wǎng)頁授權問題淺析

    微信公眾號OAuth2.0網(wǎng)頁授權問題淺析

    根據(jù)需求,我今天完成的是微信的網(wǎng)頁授權然后拉取用戶的一些基本信息的問題。具體內(nèi)容詳情大家通過本文學習吧
    2017-01-01
  • Swoole?webSocket消息服務系統(tǒng)代碼設計詳解

    Swoole?webSocket消息服務系統(tǒng)代碼設計詳解

    這篇文章主要為大家介紹了Swoole?webSocket消息服務系統(tǒng)代碼設計詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • php 運算符與表達式詳細介紹

    php 運算符與表達式詳細介紹

    運算符和表達式是php學習中最基礎的知識,本文章通過實例向大家講解一下運算符和表達式在php中的一些應用和技巧,以及需要注意的地方,需要的朋友可以參考一下。
    2016-11-11
  • laravel 模型查詢按照whereIn排序的示例

    laravel 模型查詢按照whereIn排序的示例

    今天小編就為大家分享一篇laravel 模型查詢按照whereIn排序的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • php unlink()函數(shù)使用教程

    php unlink()函數(shù)使用教程

    最近在寫個網(wǎng)站,需要上傳圖片,如果修改圖片,就圖片就沒有用了,會占用服務器的硬盤資源,所以想到用unlink函數(shù)刪除舊照片.下面腳本之家小編給大家?guī)砹藀hp unlink()函數(shù)使用教程,感興趣的朋友一起看看吧
    2018-07-07
  • WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看

    WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看

    這篇文章主要介紹了WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看的方法,可以根據(jù)SEO的需要來進行調(diào)整,需要的朋友可以參考下
    2015-12-12
  • ThinkPHP應用模式擴展詳解

    ThinkPHP應用模式擴展詳解

    這篇文章主要介紹了ThinkPHP應用模式擴展,需要的朋友可以參考下
    2014-07-07
  • php記錄搜索引擎爬行記錄的實現(xiàn)代碼

    php記錄搜索引擎爬行記錄的實現(xiàn)代碼

    這篇文章主要介紹了php記錄搜索引擎爬行記錄的實現(xiàn)代碼,然后在文中給大家補充介紹了php獲取各搜索蜘蛛爬行記錄的代碼,需要的朋友可以參考下
    2018-03-03
  • PHP上傳文件及圖片到七牛的方法

    PHP上傳文件及圖片到七牛的方法

    這篇文章主要介紹了PHP上傳文件及圖片到七牛的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • PHP實現(xiàn)的微信APP支付功能示例【基于TP5框架】

    PHP實現(xiàn)的微信APP支付功能示例【基于TP5框架】

    這篇文章主要介紹了PHP實現(xiàn)的微信APP支付功能,結(jié)合實例形式分析了基于TP5框架的微信APP支付相關原理、操作步驟與處理技巧,需要的朋友可以參考下
    2019-09-09

最新評論