码迷,mamicode.com
首页 > 其他好文 > 详细

yaf框架封装简单的pdo类

时间:2018-06-27 10:23:29      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:ado   app   随机   exe   查询   blog   tar   分享   封装   

技术分享图片<?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;
    }
    }

yaf框架封装简单的pdo类

标签:ado   app   随机   exe   查询   blog   tar   分享   封装   

原文地址:http://blog.51cto.com/13346331/2133017

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!