<?php
/**
- Created by PhpStorm.
- User: Administrator
- Date: 2017/7/24
- 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;
}
}
原文地址:http://blog.51cto.com/13346331/2133017