首页 > 作文

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

更新时间:2023-04-06 15:05:08 阅读: 评论:0

本文实例讲述了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 $urname   * @param string $password   * @param string $dbname   * @param string $chart   */  private function __construct($host, $urname, $password, $dbname, $chart)  {    //初始化数据连接    try {      $dns = 'mysql:dbname=' . $dbname . ';host=' . $host;      $this->db = new pdo($dns, $urname, $password, array(pdo::attr_persistent => true, pdo::attr_autocommit => 1));      $this->db->query('t names ' . $chart);    } catch (pdoexception $e) {      echo header("content-type: text/html; chart=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;    $urname = $config->urname;    $password = $config->password;    $dbname = $config->dbname;    $chart = $config->chart;    $db = new lf($host, $urname, $password, $dbname, $chart);    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 inrt($table, array $bind)  {    $cols = array();    $vals = array();    foreach ($bind as $k => $v) {      $cols[] = '`' . $k . '`';      $vals[] = ':' . $k;      unt($bind[$k]);      $bind[':' . $k] = $v;    }    $sql = 'inrt 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->rowcountlol屏幕分辨率();    return $res;  }  /**   * 修改   *   * @param string $table   * @param array $bind   * @param string $where   * @return boolean   */  public function update($table, array $bind, $where = '')  {    $t = array();    foreach ($bind as $k => $v) {      $bind[':' . $k] = $v;      $v = ':' . $k;      $t[] = $k . ' = ' . $v;      unt($bind[$k]);    }    $where = $this->_whereexpr($where);    $sql = 'update '      . $table      . ' t ' . implode(',', $t)      . (($where) ?怎样填写高考志愿 ' where ' . $where : '');    $stmt = $this->query($sql, $bind);    $res = $stmt->rowcount();    return $res;  }  /**   * 获取新增id   *   * @param string $tablename   * @param string $primarykey   * @return string   */  public function lastinrtid()  {    return (string) $this->db->lastinrtid();  }  public function query($sql, $bind = array())  {    if(!is_array($bind)){      $bind = array($bind);    }    $stmt = $this->prepare($sql);    $stmt->execute($bind);    $stmt->tfetchmode($this->_fetchmode);    return $stmt;  }  public function prepare($sql = '')  {    if(empty($sql)){      return fal;    }    $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) {        r尉迟迥eturn $e->getmessage();      }    }el {      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 clo()  {    $this->_db = null;  }}

配置

db.type = 'mysql'db.host = '127.0.0.1'db.urname = 'root'db.password = '123456'db.dbname = 'test'db.chart = 'utf8'

调用方法

class testcontroller extends yaf_controller_abstract{  public function indexaction()  {    $config = yaf_application::app()->getconfig()->db;    $db = db_mysql::getinstance($config);    $row = $db->fetchone('lect count(*) from `ur`');    print_r($row);die;  }}

结果

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

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

本文发布于:2023-04-06 15:05:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/d003fb9fc880c1e4e4c8a10b3d5ea3ae.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Yaf框架封装的MySQL数据库操作示例.doc

本文 PDF 下载地址:Yaf框架封装的MySQL数据库操作示例.pdf

下一篇:返回列表
标签:框架   入门教程   操作   数据库
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图