yaf mysql pdo 封装_yaf框架封装简单的pdo类

Time: 14:03

*/

/* 数据库PDO操作

*/

class pdo {

private function Link(){

$config=Yaf_Application::app()->getConfig();

try{

$PDO=new PDO($config[‘mysql‘][‘dsn‘],$config[‘mysql‘][‘user‘],$config[‘mysql‘][‘pass‘]);

return $PDO;

}

catch (Exception $e){

echo $e->getMessage();

}

}

//查询单条数据

public function quOne($sql,$term=‘‘){

$PDO=$this->Link();

try{

$stmt = $PDO->prepare($sql);

if(!empty($term)){

foreach ($term as $key=>$value){

$stmt->bindParam($key, $value, PDO::PARAM_STR);

}

}

$stmt->execute();

return $stmt->fetch(PDO::FETCH_ASSOC);

} // if there is a problem we can handle it here

catch (Exception $e) {

echo $e->getMessage();

}

}

//查询所有数据

public function quAll($sql,$term=‘‘){

$PDO=$this->Link();

try{

$stmt = $PDO->prepare($sql);

if(!empty($term)){

foreach ($term as $key=>$value){

$stmt->bindParam($key, $value, PDO::PARAM_STR);

}

}

$stmt->execute();

return $stmt->fetchAll(PDO::FETCH_ASSOC);

} // if there is a problem we can handle it here

catch (Exception $e) {

echo $e->getMessage();

}

}

//插入单条数据

public function inOne($name,$data,$id="",$statc=true){

if(!$statc){

return false;

}

if(!is_array($data)){

return false;

}

$PDO=$this->Link();

foreach ($data as $key=>$value){

$keys[]=$key;

$values[]=$value;

}

$sql="insert into ".$name." (";

$sql.=implode(",",$keys);

$sql.=",_str";

$sql.=") values (";

$sql.implode(",",$values);

$_str=time().$this->getrandstr();

$sql.=",‘$_str‘";

$sql.=" )";

try{

$stmt = $PDO->prepare($sql);

$stmt->beginTransaction();

$stmt->execute();

if(empty($id)){

return true;

}

$insql="select $id from $name where _str = ‘$_str‘";

$InsertId = $PDO->prepare($insql);

$InsertId->execute();

return $InsertId->fetch(PDO::FETCH_ASSOC);

} // if there is a problem we can handle it here

catch (Exception $e) {

$PDO->rollBack();

return false;

}

}

//插入多条语句

public function inAll($name,$data,$id="",$statc=true){

if(!$statc){

return false;

}

if(!is_array($data)){

return false;

}

$PDO=$this->Link();

$valueStr=‘‘;

foreach ($data as $key=>$value){

if(!is_array($value)){

return false;

}

$values=‘‘;

$keys=‘‘;

$valueStr.="(";

foreach ($value as $k=>$v){

$values[]=$v;

$keys[]=$k;

}

$_str=time().$this->getrandstr();

$_strs[]=$_str;

$keys[]="_str";

$values[]=$_str;

$valueStr.=implode(",",$values).")";

}

$sql="insert into ".$name." (";

$sql.=implode(",",$keys);

$sql.=",_str";

$sql.=") values ".$valueStr;

try{

$PDO->beginTransaction();

$stmt = $PDO->prepare($sql);

$stmt->execute();

if(empty($id)){

return true;

}

$_strTerm=implode($_str);

$insql="select $id from $name where _str in ($_strTerm)";

$InsertId = $PDO->prepare($insql);

$InsertId->execute();

return $InsertId->fetchAll(PDO::FETCH_ASSOC);

} // if there is a problem we can handle it here

catch (Exception $e) {

$PDO->rollBack();

return false;

}

}

//删除

public function Del ($sql,$statc=true){

if(!$statc){

return false;

}

$PDO=$this->Link();

try{

$PDO->beginTransaction();

$stmt = $PDO->prepare($sql);

if(!empty($term)){

foreach ($term as $key=>$value){

$stmt->bindParam($key, $value, PDO::PARAM_STR);

}

}

$stmt->execute();

return true;

} // if there is a problem we can handle it here

catch (Exception $e) {

$PDO->rollBack();

return false;

}

}

//修改

public function Update ($sql,$statc=true){

if(!$statc){

return false;

}

$PDO=$this->Link();

try{

$PDO->beginTransaction();

$stmt = $PDO->prepare($sql);

if(!empty($term)){

foreach ($term as $key=>$value){

$stmt->bindParam($key, $value, PDO::PARAM_STR);

}

}

$stmt->execute();

return true;

} // if there is a problem we can handle it here

catch (Exception $e) {

$PDO->rollBack();

return false;

}

}

//获取随机字符

private function getrandstr(){

$str=‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890‘;

$randStr = str_shuffle($str);//打乱字符串

$rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分

return $rands;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值