最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Yaf框架封裝的MySQL數(shù)據(jù)庫操作示例

 更新時間:2019年03月06日 16:39:50   作者:doomsday0417  
這篇文章主要介紹了Yaf框架封裝的MySQL數(shù)據(jù)庫操作,結(jié)合實例形式分析了Yaf框架基于PDO操作MySQL數(shù)據(jù)庫的相關(guān)配置、連接、增刪改查、統(tǒng)計等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Yaf框架封裝的MySQL數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:

Yaf封裝DB簡單操作

介紹

因為Yaf是一個純天然的MVC闊架,本人還在貝銳的時候就和主管一起用Yaf框架去重構(gòu)了向日葵的網(wǎng)站端,到后面,Yaf也逐漸應(yīng)用到了其他項目上,但是Yaf是沒有帶DB類庫的,所以本人也共享下最近封裝的代碼!

代碼

使用PDO封裝MySQL操作

class Db_Mysql
{
  private $_options = array();
  private $db;
  private $statement;
  private $_fetchMode = 2;
  /**
   * 構(gòu)造函數(shù)
   *
   * @param string $host
   * @param string $username
   * @param string $password
   * @param string $dbname
   * @param string $charset
   */
  private function __construct($host, $username, $password, $dbname, $charset)
  {
    //初始化數(shù)據(jù)連接
    try {
      $dns = 'mysql:dbname=' . $dbname . ';host=' . $host;
      $this->db = new PDO($dns, $username, $password, array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_AUTOCOMMIT => 1));
      $this->db->query('SET NAMES ' . $charset);
    } catch (PDOException $e) {
      echo header("Content-type: text/html; charset=utf-8");
      echo '<pre />';
      echo '<b>Connection failed:</b>' . $e->getMessage();
      die;
    }
  }
  /**
   * 調(diào)用初始化MYSQL連接
   *
   * @param string $config
   * @return Aomp_Db_Mysql
   */
  static public function getInstance($config = '')
  {
    $host = $config->host;
    $username = $config->username;
    $password = $config->password;
    $dbname = $config->dbname;
    $charset = $config->charset;
    $db = new self($host, $username, $password, $dbname, $charset);
    return $db;
  }
  /**
   * 獲取多條數(shù)據(jù)
   *
   * @param string $sql
   * @param array $bind
   * @param string $fetchMode
   * @return multitype:
   */
  public function fetchAll($sql, $bind = array(), $fetchMode = null)
  {
    if($fetchMode === NULL){
      $fetchMode = $this->_fetchMode;
    }
    $stmt = $this->query($sql, $bind);
    $res = $stmt->fetchAll($fetchMode);
    return $res;
  }
  /**
   * 獲取單條數(shù)據(jù)
   *
   * @param string $sql
   * @param array $bind
   * @param string $fetchMode
   * @return mixed
   */
  public function fetchRow($sql, array $bind = array(), $fetchMode = null)
  {
    if ($fetchMode === null) {
      $fetchMode = $this->_fetchMode;
    }
    $stmt = $this->query($sql, $bind);
    $result = $stmt->fetch($fetchMode);
    return $result;
  }
  /**
   * 獲取統(tǒng)計或者ID
   *
   * @param string $sql
   * @param array $bind
   * @return string
   */
  public function fetchOne($sql, array $bind = array())
  {
    $stmt = $this->query($sql, $bind);
    $res = $stmt->fetchColumn(0);
    return $res;
  }
  /**
   * 增加
   *
   * @param string $table
   * @param array $bind
   * @return number
   */
  public function insert($table, array $bind)
  {
    $cols = array();
    $vals = array();
    foreach ($bind as $k => $v) {
      $cols[] = '`' . $k . '`';
      $vals[] = ':' . $k;
      unset($bind[$k]);
      $bind[':' . $k] = $v;
    }
    $sql = 'INSERT INTO '
      . $table
      . ' (' . implode(',', $cols) . ') '
      . 'VALUES (' . implode(',', $vals) . ')';
    $stmt = $this->query($sql, $bind);
    $res = $stmt->rowCount();
    return $res;
  }
  /**
   * 刪除
   *
   * @param string $table
   * @param string $where
   * @return boolean
   */
  public function delete($table, $where = '')
  {
    $where = $this->_whereExpr($where);
    $sql = 'DELETE FROM '
      . $table
      . ($where ? ' WHERE ' .$where : '');
    $stmt = $this->query($sql);
    $res = $stmt->rowCount();
    return $res;
  }
  /**
   * 修改
   *
   * @param string $table
   * @param array $bind
   * @param string $where
   * @return boolean
   */
  public function update($table, array $bind, $where = '')
  {
    $set = array();
    foreach ($bind as $k => $v) {
      $bind[':' . $k] = $v;
      $v = ':' . $k;
      $set[] = $k . ' = ' . $v;
      unset($bind[$k]);
    }
    $where = $this->_whereExpr($where);
    $sql = 'UPDATE '
      . $table
      . ' SET ' . implode(',', $set)
      . (($where) ? ' WHERE ' . $where : '');
    $stmt = $this->query($sql, $bind);
    $res = $stmt->rowCount();
    return $res;
  }
  /**
   * 獲取新增ID
   *
   * @param string $tableName
   * @param string $primaryKey
   * @return string
   */
  public function lastInsertId()
  {
    return (string) $this->db->lastInsertId();
  }
  public function query($sql, $bind = array())
  {
    if(!is_array($bind)){
      $bind = array($bind);
    }
    $stmt = $this->prepare($sql);
    $stmt->execute($bind);
    $stmt->setFetchMode($this->_fetchMode);
    return $stmt;
  }
  public function prepare($sql = '')
  {
    if(empty($sql)){
      return false;
    }
    $this->statement = $this->db->prepare($sql);
    return $this->statement;
  }
  public function execute($param = '')
  {
    if(is_array($param)){
      try {
        return $this->statement->execute($param);
      } catch (Exception $e) {
        return $e->getMessage();
      }
    }else {
      try {
        return $this->statement->execute();
      } catch (Exception $e) {
        return $e->getMessage();
      }
    }
  }
  /**
   *
   * @param string $where
   * @return null|string
   */
  protected function _whereExpr($where)
  {
    if(empty($where)){
      return $where;
    }
    if(!is_array($where)){
      $where = array($where);
    }
    $where = implode(' AND ', $where);
    return $where;
  }
  /**
   * 關(guān)閉數(shù)據(jù)庫操作
   */
  public function close()
  {
    $this->_db = null;
  }
}

配置

db.type = 'mysql'
db.host = '127.0.0.1'
db.username = 'root'
db.password = '123456'
db.dbname = 'test'
db.charset = 'UTF8'

調(diào)用方法

class TestController extends Yaf_Controller_Abstract
{
  public function indexAction()
  {
    $config = Yaf_Application::app()->getConfig()->db;
    $db = Db_Mysql::getInstance($config);
    $row = $db->fetchOne('select count(*) from `user`');
    print_r($row);die;
  }
}

結(jié)果

更多關(guān)于php框架相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php優(yōu)秀開發(fā)框架總結(jié)》、《codeigniter入門教程》、《ThinkPHP入門教程》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • php 文件上傳實例代碼

    php 文件上傳實例代碼

    php 文件上傳實例代碼,php中上傳文件就比asp的容易多了,代碼量比較少
    2012-04-04
  • 給ECShop添加最新評論

    給ECShop添加最新評論

    這篇文章主要介紹了給ECShop添加最新評論的方法及代碼分享,需要的朋友可以參考下
    2015-01-01
  • ThinkPHP的截取字符串函數(shù)無法顯示省略號的解決方法

    ThinkPHP的截取字符串函數(shù)無法顯示省略號的解決方法

    這篇文章主要介紹了ThinkPHP的截取字符串函數(shù)無法顯示省略號的解決方法,需要的朋友可以參考下
    2014-06-06
  • PHP開發(fā)Apache服務(wù)器配置

    PHP開發(fā)Apache服務(wù)器配置

    這篇文章主要介紹了PHP開發(fā)Apache服務(wù)器配置的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • Yii框架上傳圖片用法總結(jié)

    Yii框架上傳圖片用法總結(jié)

    這篇文章主要介紹了Yii框架上傳圖片用法,結(jié)合實例形式總結(jié)分析了Yii框架上傳圖片的相關(guān)注意事項與使用技巧,需要的朋友可以參考下
    2016-03-03
  • Yii 框架使用Gii生成代碼操作示例

    Yii 框架使用Gii生成代碼操作示例

    這篇文章主要介紹了Yii 框架使用Gii生成代碼操作,結(jié)合實例形式F分析了Yii 使用Gii生成代碼基本操作步驟與相關(guān)注意事項,需要的朋友可以參考下
    2020-05-05
  • php生成html文件方法總結(jié)

    php生成html文件方法總結(jié)

    本文這里匯總了一些自己使用的和網(wǎng)上搜集來的php生成html靜態(tài)文件的方法,非常的使用,也分析了他們的優(yōu)缺點,是篇非常不錯的文章,這里推薦給大家。
    2014-12-12
  • PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實例講解)

    PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實例講解)

    下面小編就為大家?guī)硪黄狿HP二維關(guān)聯(lián)數(shù)組的遍歷方式(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • PHP中strlen()和mb_strlen()的區(qū)別淺析

    PHP中strlen()和mb_strlen()的區(qū)別淺析

    這篇文章主要介紹了PHP中strlen()和mb_strlen()的區(qū)別淺析,本文探討的中英混合的字符在使用這個函數(shù)時的區(qū)別,需要的朋友可以參考下
    2014-06-06
  • 用 Composer構(gòu)建自己的 PHP 框架之設(shè)計 MVC

    用 Composer構(gòu)建自己的 PHP 框架之設(shè)計 MVC

    幾乎所有人都是通過學(xué)習(xí)某個框架來了解 MVC 的,這樣可能框架用的很熟,一旦離了框架一個簡單的頁面都寫不了,更不要說自己設(shè)計 MVC 架構(gòu)了,其實這里面也沒有那么多門道,原理非常清晰
    2014-10-10

最新評論

伊宁县| 吴忠市| 久治县| 南木林县| 西乌珠穆沁旗| 九龙城区| 怀化市| 浙江省| 兴山县| 牟定县| 保德县| 油尖旺区| 崇信县| 潮安县| 大埔县| 南京市| 铜梁县| 曲靖市| 夏津县| 乌恰县| 安宁市| 荥阳市| 卓资县| 九江市| 临猗县| 耒阳市| 离岛区| 盐山县| 锦州市| 健康| 漳州市| 靖江市| 阜城县| 比如县| 自治县| 浦北县| 柳林县| 日喀则市| 类乌齐县| 东乡县| 平潭县|