一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - Yaf框架封裝的MySQL數(shù)據(jù)庫操作示例

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

2019-06-05 11:08doomsday0417服務(wù)器之家 PHP教程

這篇文章主要介紹了Yaf框架封裝的MySQL數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了Yaf框架基于PDO操作MySQL數(shù)據(jù)庫的相關(guān)配置、連接、增刪改查、統(tǒng)計(jì)等相關(guān)操作技巧,需要的朋友可以參考下

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

Yaf封裝DB簡單操作

介紹

因?yàn)閅af是一個(gè)純天然的MVC闊架,本人還在貝銳的時(shí)候就和主管一起用Yaf框架去重構(gòu)了向日葵的網(wǎng)站端,到后面,Yaf也逐漸應(yīng)用到了其他項(xià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)計(jì)或者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é)果

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产乱码一卡二卡3卡四卡 国产乱插 | 大乳孕妇一级毛片 | 国产九九 | 日韩国产欧美成人一区二区影院 | 91四虎国自产在线播放线 | pron欧美| 2020国产精品永久在线观看 | 能播放的欧美同性videos | 香蕉久久网 | a级片在线观看免费 | 三级黄色片在线观看 | 四虎最新永久免费网址 | 狠狠色综合久久久久尤物 | 色婷婷六月天 | 91精品国产高清久久久久久 | 高清不卡一区二区 | 大陆男男gayxxxxvideo | 给我视频免费看 | 国产高清好大好夹受不了了 | 国产精品久久香蕉免费播放 | 动漫人物差差差动漫人物免费观看 | 91久久偷偷做嫩草影院免费看 | 久久久免费观看 | 亚洲欧美另类第一页 | 我和么公的秘密小说免费 | 视频一区在线免费观看 | 亚洲一二区视频 | 女人与d0gxxx| 2020年国产精品午夜福利在线观看 | 国产91在线精品 | 爱情岛论坛亚洲自拍 | 男女乱淫真视频播放网站 | 视频精品一区二区三区 | juy_661佐佐木明希在线播放 | 精品亚洲综合在线第一区 | 99久久精品国产片久人 | 日本亚洲欧洲高清有码在线播放 | 免费视频观看 | 欧美一级片在线视频 | 齐天大性之七仙女欲春迅雷链接 | 超碰97 |