yaf框架封装简单的pdo类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yaf框架封装简单的pdo类相关的知识,希望对你有一定的参考价值。

技术分享图片<?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类的主要内容,如果未能解决你的问题,请参考以下文章

PDO 封装类

[PHP] Yaf框架的简单安装使用

关于Yaf框架的使用总结

封装好的PDO类

PHP 基于PDO封装CURD,分页类封装

Yaf框架下类的自动加载