yaf mysql_Yaf框架封装的MySQL数据库操作示例
本文实例讲述了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 '
';
echo 'Connection failed:' . $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;
}
}
结果
希望本文所述对大家PHP程序设计有所帮助。
yaf mysql_Yaf框架封装的MySQL数据库操作示例相关推荐
- python爬虫开发数据库设计入门经典_Python3实现的爬虫爬取数据并存入mysql数据库操作示例...
本文实例讲述了Python3实现的爬虫爬取数据并存入mysql数据库操作.分享给大家供大家参考,具体如下: 爬一个电脑客户端的订单.罗总推荐,抓包工具用的是HttpAnalyzerStdV7,与chr ...
- python爬取新闻并归数据库_Python爬取数据并写入MySQL数据库操作示例
Python爬取数据并写入MySQL数据库的实例 首先我们来爬取 http://html-color-codes.info/color-names/ 的一些数据. 按 F12 或 ctrl+u 审查元 ...
- python web框架flask连接mysql数据库操作
最近在用flask框架做一个python web项目,其中要连接数据库,所以总结如下: 如何用pycharm快速创建一个flask项目我就不详细说了,可以先创建虚拟环境,不过大部分的事pycharm都 ...
- mysql数据库访问程序_c++程序访问MySQL数据库操作示例
1.安装mysql sudo apt-get install mysql-server mysql-client 安装过程中会提示设置用户名和密码 2.启动mysql sudo /etc/init.d ...
- freecplus框架-MySQL数据库操作
文章目录 一.源代码说明 二.概述 三.connection类 四.sqlstatement类 五.程序流程 1.无结果集SQL的程序的流程 2.有结果集SQL的程序的流程 六.示例程序 1.创建超女 ...
- php操作mysql的封装类_PHP封装的mysqli数据库操作类示例
本文实例讲述了PHP封装的mysqli数据库操作类.分享给大家供大家参考,具体如下: 由于PHPMySQL操作在PHP5.4以下被弃用而推荐使用mysqli(MySQL Improvement),这里 ...
- html checkbox 多选 根据数据库来显示选取和未选取,前端框架(2)DIV多选复选框框的封装和MySql数据库存取...
前端框架(二)DIV多选复选框框的封装和MySql数据库存取 图可以包含的寓意和含义是文字不能比拟的,先有一个效果图你也就知道这篇文章的主要内容是关于什么问题的,省去了一大堆文字的累述,看下面这张图: ...
- Python封装MySQL数据库操作(pymysql)
Python封装MySQL数据库操作(pymysql) # 连接MySQL class DbManager(object):# 构造函数def __init__(self):self.conn = N ...
- c语言转换字符编码为zhs16gbk,GitHub - veis-lzf/freecplus: freecplus开源框架,包含了数据库操作、socket、ftp服务器等。...
一.freecplus框架介绍 freecplus框架是UNIX平台下C/C++程序开发的业务层基础框架,由C语言技术网组织开发.维护.其目的是为C/C++程序员供免费的.开源的程序库.freecpl ...
最新文章
- 关于citrix 负载及WI的一些问题
- 禁用Cookie在web浏览器中读取/写入c#应用程序
- SGU 183. Painting the balls( dp )
- JVM类加载机制详解(一)JVM类加载过程
- semantic ui要装什么才能使用
- python火爆的原因_为什么Python这么火爆?原因是什么?
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)-过滤器的使用和批量删除数据(伪删除和直接删除)...
- 【Flink】Flink报错OutofMemoryError : Direct buffer memory
- Spark提交任务到集群
- SpringCloud + RocketMQ实现可靠消息最终一致性事务
- 电脑运行速度慢的处理
- android sd卡名称,科普详解Android系统SD卡各类文件夹名称
- Maven的作用到底是什么?
- Rocketmq同项目多个生产者多个消费者问题
- 如何检索论文时让两个词在一块而不是分开?
- 计算机视觉有哪些SCI期刊? - 易智编译EaseEditing
- 【CET4四级英语】【单词】单词的同义替换(1)
- 分享自己做的一个词典小软件
- 常见功能点测试用例归纳总结
- Karl Guttag:我在AWE 2022体验到了哪些有意思的AR眼镜
热门文章
- 如何在页面上减少用户操作?
- 2016中国大数据大会成功举办 重磅发布年度大数据十大热词
- Linux使用vim编辑文本时字体背景颜色变为黄色 解决办法
- 红米手机可以用什么蓝牙耳机?高性价比蓝牙耳机推荐
- 如何搭建网站赚钱?这篇网上赚钱攻略,对你或许有帮助!
- [附源码]Java计算机毕业设计SSM大学请假管理系统
- qtextedit固定内容_Qt中QTextEdit使用小技巧
- 解决Visio中可编辑的流程图复制到word/wps中却显示不完全的问题
- Eclipse (indigo) 中安装jdk包并执行Maven
- Excel隐藏当前工作表的2种操作