Yaf框架封装的MySQL数据库操作示例_第1页
Yaf框架封装的MySQL数据库操作示例_第2页
Yaf框架封装的MySQL数据库操作示例_第3页
Yaf框架封装的MySQL数据库操作示例_第4页
Yaf框架封装的MySQL数据库操作示例_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论