欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Yaf框架封装的MySQL数据库操作示例

程序员文章站 2022-05-14 14:19:27
本文实例讲述了yaf框架封装的mysql数据库操作。分享给大家供大家参考,具体如下: yaf封装db简单操作 介绍 因为yaf是一个纯天然的mvc阔架,本人还在贝锐的...

本文实例讲述了yaf框架封装的mysql数据库操作。分享给大家供大家参考,具体如下:

yaf封装db简单操作

介绍

因为yaf是一个纯天然的mvc阔架,本人还在贝锐的时候就和主管一起用yaf框架去重构了向日葵的网站端,到后面,yaf也逐渐应用到了其他项目上,但是yaf是没有带db类库的,所以本人也共享下最近封装的代码!

代码

使用pdo封装mysql操作

class db_mysql
{
  private $_options = array();
  private $db;
  private $statement;
  private $_fetchmode = 2;
  /**
   * 构造函数
   *
   * @param string $host
   * @param string $username
   * @param string $password
   * @param string $dbname
   * @param string $charset
   */
  private function __construct($host, $username, $password, $dbname, $charset)
  {
    //初始化数据连接
    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;
    }
  }
  /**
   * 调用初始化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;
  }
  /**
   * 获取多条数据
   *
   * @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;
  }
  /**
   * 获取单条数据
   *
   * @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;
  }
  /**
   * 获取统计或者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;
  }
  /**
   * 关闭数据库操作
   */
  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'

调用方法

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;
  }
}

结果

Yaf框架封装的MySQL数据库操作示例

更多关于php框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》、《codeigniter入门教程》、《thinkphp入门教程》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。