版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Yaf框架封装的MySQL数据库操作示例本文实例讲述了Yaf框架封装的MySQL数据库操作。分享给大家供大家参考,具体如下:
Yaf封装DB简单操作
介绍
因为Yaf是一个纯天然的MVC阔架,本人还在贝锐的时候就和主管一起用Yaf框架去重构了向日葵的网站端,到后面,Yaf也逐渐应用到了其他项目上,但是Yaf是没有带DB类库的,所以本人也共享下最近封装的代码!
代码
使用PDO封装MySQL操作
classDb_Mysql
private$_options=array();
private$db;
private$statement;
private$_fetchMode=2;
*构造函数
*@paramstring$host
*@paramstring$username
*@paramstring$password
*@paramstring$dbname
*@paramstring$charset
privatefunction__construct($host,$username,$password,$dbname,$charset)
//初始化数据连接
try{
$dns='mysql:dbname='.$dbname.';host='.$host;
$this-db=newPDO($dns,$username,$password,array(PDO::ATTR_PERSISTENT=true,PDO::ATTR_AUTOCOMMIT=1));
$this-db-query('SETNAMES'.$charset);
}catch(PDOException$e){
echoheader("Content-type:text/html;charset=utf-8");
echo'pre/
echo'bConnectionfailed:/b'.$e-getMessage();
die;
*调用初始化MYSQL连接
*@paramstring$config
*@returnAomp_Db_Mysql
staticpublicfunctiongetInstance($config='')
$host=$config-host;
$username=$config-username;
$password=$config-password;
$dbname=$config-dbname;
$charset=$config-charset;
$db=newself($host,$username,$password,$dbname,$charset);
return$db;
*获取多条数据
*@paramstring$sql
*@paramarray$bind
*@paramstring$fetchMode
*@returnmultitype:
publicfunctionfetchAll($sql,$bind=array(),$fetchMode=null)
if($fetchMode===NULL){
$fetchMode=$this-_fetchMode;
$stmt=$this-query($sql,$bind);
$res=$stmt-fetchAll($fetchMode);
return$res;
*获取单条数据
*@paramstring$sql
*@paramarray$bind
*@paramstring$fetchMode
*@returnmixed
publicfunctionfetchRow($sql,array$bind=array(),$fetchMode=null)
if($fetchMode===null){
$fetchMode=$this-_fetchMode;
$stmt=$this-query($sql,$bind);
$result=$stmt-fetch($fetchMode);
return$result;
*获取统计或者ID
*@paramstring$sql
*@paramarray$bind
*@returnstring
publicfunctionfetchOne($sql,array$bind=array())
$stmt=$this-query($sql,$bind);
$res=$stmt-fetchColumn(0);
return$res;
*增加
*@paramstring$table
*@paramarray$bind
*@returnnumber
publicfunctioninsert($table,array$bind)
$cols=array();
$vals=array();
foreach($bindas$k=$v){
$cols[]='`'.$k.'`';
$vals[]=':'.$k;
unset($bind[$k]);
$bind[':'.$k]=$v;
$sql='INSERTINTO'
.$table
.'('.implode(',',$cols).')'
.'VALUES('.implode(',',$vals).')';
$stmt=$this-query($sql,$bind);
$res=$stmt-rowCount();
return$res;
*删除
*@paramstring$table
*@paramstring$where
*@returnboolean
publicfunctiondelete($table,$where='')
$where=$this-_whereExpr($where);
$sql='DELETEFROM'
.$table
.($where'WHERE'.$where:'');
$stmt=$this-query($sql);
$res=$stmt-rowCount();
return$res;
*修改
*@paramstring$table
*@paramarray$bind
*@paramstring$where
*@returnboolean
publicfunctionupdate($table,array$bind,$where='')
$set=array();
foreach($bindas$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
*@paramstring$tableName
*@paramstring$primaryKey
*@returnstring
publicfunctionlastInsertId()
return(string)$this-db-lastInsertId();
publicfunctionquery($sql,$bind=array())
if(!is_array($bind)){
$bind=array($bind);
$stmt=$this-prepare($sql);
$stmt-execute($bind);
$stmt-setFetchMode($this-_fetchMode);
return$stmt;
publicfunctionprepare($sql='')
if(empty($sql)){
returnfalse;
$this-statement=$this-db-prepare($sql);
return$this-statement;
publicfunctionexecute($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();
*@paramstring$where
*@returnnull|string
protectedfunction_whereExpr($where)
if(empty($where)){
return$where;
if(!is_array($where)){
$where=array($where);
$where=implode('AND',$where);
return$where;
*关闭数据库操作
publicfunctionclose()
$this-_db=null;
配置
db.type='mysql'
db.host='127.0.0.1'
db.username='root'
db.password='123456'
db.dbname='test'
db.charset='UTF8'
调用方法
classTestControllerextendsYaf_Controller_Abstract
publicfunctionindexAction()
$config=Yaf_Application::app()-getConfig()-
$db=Db_Mysql::getInstance($config);
$row
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 骨科护理技术操作规范
- 大肠息肉术后运动康复护理
- 预见性护理在康复护理中的应用
- 药物应用护理中的持续质量改进
- 内镜间质瘤患者的心理护理与支持
- 南京公务员试题及答案
- 教案-单元六任务1证件照制作-美图秀秀
- (二)安全用电试卷附答案
- 刨花板铺装工核心实操评优考核试卷含答案
- 薪税师合规测试考核试卷含答案
- 作文格子纸(小学生专用Word版)
- 八年级地理第三次月考试卷
- 贵州义华实业有限责任公司煤矸石提硫建设项目环评报告
- 个人嘉奖登记(报告)表(无水印)
- 泌乳奶牛营养需要
- 水利工程施工监理规范SL288-2014标准参考
- DL-T 1083-2019 火力发电厂分散控制系统技术条件
- 六三制新青岛版四年级下册科学第16课《动物的“家”》课件
- LY/T 2810-2017结构化森林经营技术规程
- GB/T 14996-2010高温合金冷轧板
- 人工起道捣固作业考评表(工务专业)
评论
0/150
提交评论