VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PDO操作MySql类分享

为了让自己的数据类能够做到最大化的重用,就写个能够重用的PDO操作MySql的类,由于pdo可以连接现在流行的各种数据库,所以单独的写个配置类类来完成不同数据库DSN的配置,PDO操作MYSQL类代码如下:

  1. <?php 
  2. /** 
  3.  * 类标准说明    PDO连接数据库的配置类 
  4.  * 类名:     ConfigDataBase 
  5.  * 功能说明:    为了让代码重用,利用此类可以动态的连接各种数据库 
  6.  * 参数说明:    $_dbms = "mysql教程";    //数据库类型  
  7.  *         $_host = '127.0.0.1';     //数据库ip地址 
  8.  *         $_port = '3306';     //数据库端口 
  9.  *         $_username = 'root';    //数据库用户名 
  10.  *        $_password = 'liujijun';   //密码 
  11.  *         $_dbname = 'zendf';        //数据库名 默认为zenf 
  12.  *         $_charset = 'utf-8';       //数据库字符编码 
  13.  *         $_dsn;//                    //data soruce name 数据源 
  14.  * 
  15.  * 
  16.  * 类属性说明: 
  17.  * 类方法说明: 
  18.  * 返回值:     不同函数返回不同的值 
  19.  * 备注说明: 
  20.  * 作者:       刘纪君 
  21.  * 最后一次修改时间:    2011下午02:01:39 
  22.  * 
  23.  */ 
  24. class ConfigDataBase { 
  25.    
  26.  protected static $_dbms = "mysql";    //数据库类型  
  27.     protected static $_host = '127.0.0.1';     //数据库ip地址 
  28.     protected static $_port = '3306';     //数据库端口 
  29.     protected static $_username = 'root';    //数据库用户名 
  30.     protected static $_password = 'liujijun';   //密码 
  31.     protected static $_dbname = 'zendf';        //数据库名 默认为zenf 
  32.     protected static $_charset = 'utf-8';       //数据库字符编码 
  33.     protected static $_dsn;//                    //data soruce name 数据源 
  34.   
  35.  /** 
  36.   *@return   返回数据源名 
  37.   */ 
  38.  public static function getDsn() { 
  39.   //将变量的值组合成  mysql:host=localhost;port =3306;dbname=test',$login,$passwd的形式 
  40.   if (!isset(self::$_dsn)){ 
  41.     self::$_dsn = self::$_dbms.':host = '.self::$_host.';prot = '. 
  42.     self::$_port . ';dbname = ' . self::$_dbname.','. 
  43.     self::$_username . ','.self::$_password; 
  44.    
  45.     if (strlen(self::$_charset) > 0){ 
  46.      self::$_dsn = self::$_dsn . ';charset = ' . self::$_charset; 
  47.     } 
  48.   } 
  49.   return self::$_dsn;//返回数据源名 
  50.  } 
  51.   
  52.  /** 
  53.   * 功能:设置$dbms 
  54.   * @param $dbms 
  55.   */ 
  56.  public static function setDbms($dbms){ 
  57.   if (isset($dbms) &&(strlen($dbms) > 0 )){ 
  58.    self::$_dbms = trim($dbms); 
  59.   }  
  60.  } 
  61.   
  62.  /** 
  63.   * 
  64.   * @param  $host  //数据库地址 
  65.   */ 
  66.  public static function setHost($host){ 
  67.   if (isset($host) &&(strlen($host) > 0 )){ 
  68.    self::$_host = trim($host); 
  69.   } 
  70.  } 
  71.  
  72.  /** 
  73.   * 
  74.   * @param $host 端口号 
  75.   */ 
  76.  public static function setPort($port){ 
  77.   if (isset($port) &&(strlen($port) > 0 )){ 
  78.    self::$_post = trim($port); 
  79.   }//开源代码phpfensi.com 
  80.  } 
  81.   
  82.  /** 
  83.   * 
  84.   * @param  $passwd 密码 
  85.   */ 
  86.  public static function setPasswd($passwd){ 
  87.   if (isset($passwd) &&(strlen($passwd) > 0 )){ 
  88.    self::$_password = trim($passwd); 
  89.   } 
  90.  } 
  91.   
  92.  /** 
  93.   * 
  94.   * @param  $username 用户名 
  95.   */ 
  96.  public static function setUsernName($username){ 
  97.    if (isset($username) &&(strlen($username) > 0 )){ 
  98.     self::$_username = trim($username); 
  99.    } 
  100.   } 
  101.   
  102.  /** 
  103.   * 
  104.   * @param  $dbname 数据库名 
  105.   */ 
  106.  public static function setDbName($dbname){ 
  107.    if (isset($dbname) &&(strlen($dbname) > 0 )){ 
  108.     self::$_dbname = trim($dbname); 
  109.    } 
  110.   } 
  111.   
  112.   
  113.   /** 
  114.    * 
  115.    * @param  $charset 数据库编码 
  116.    */ 
  117.  public static function setCharset($charset){ 
  118.    if (isset($charset) &&(strlen($charset) > 0 )){ 
  119.     self::$_charset = trim($charset); 
  120.    } 
  121.   } 
  122. } 
  123.  
  124. 下面是对数据库的操作: 
  125.  
  126.   
  127.  
  128. <?php 
  129.  
  130. require_once 'ConfigDataBase.php'; 
  131. header("Content-Type: text/html; charset=utf-8");//设置编码 
  132. /** 
  133.  * 类标准说明 
  134.  * 类名:      PdoMysql 
  135.  * 功能说明:     对数据库进行各种操作 
  136.  * 参数说明: 
  137.  * 类属性说明: 
  138.  * 类方法说明: 
  139.  * 返回值: 
  140.  * 备注说明: 
  141.  * 作者:       刘纪君 
  142.  * 最后一次修改时间:    2011上午10:45:36 
  143.  * 
  144.  */ 
  145. class  PdoMysqlOperater{ 
  146.   
  147.   
  148.  /** 
  149.   * @return 返回连接数据库的句柄 
  150.   */ 
  151.  public function getConnection(){ 
  152.   $connection = NULL; 
  153.   try { 
  154.    $connection = new PDO(ConfigDataBase::getDsn()); 
  155.    echo 'Success'; 
  156.   } catch (PDOException  $e) { 
  157.    print "Error in connection :".$e->getMessage().' '.die(); 
  158.   } 
  159.   return $connection; 
  160.  } 
  161.   
  162.  /** 
  163.   * 
  164.   * @param  $connection    连接数据库的句柄 
  165.   */ 
  166.  public function closeConnection($connection){ 
  167.   try { 
  168.    if ($connection != null) { 
  169.     $connection = null;//关闭数据库连接句柄 
  170.    } 
  171.   } catch (Exception $e) { 
  172.    print 'Close the connectin is error:'.$e->getMessage(); 
  173.   } 
  174.    
  175.  } 
  176.   
  177.  /** 
  178.   * 功能:      向数据库中增加数据 
  179.   * @param $sql      sql语句 
  180.   */ 
  181.  public  function insertDatabase($sql){ 
  182.   $affect = false;//失败返回false 
  183.   try { 
  184.    $conn = $this->getConnection(); 
  185.    $conn->exec($sql); 
  186.    $affect = true;//插入成功返回true 
  187.    $this->closeConnection($conn);//关闭数据库 
  188.   } catch (PDOException $e) { 
  189.    print 'Insert error '.$e->getMessage(); 
  190.   } 
  191.   return $affect;//返回值 
  192.  } 
  193.   
  194.  /** 
  195.   * 
  196.   * @param $id      表的主键id 
  197.   * @param $tableName    表名 
  198.   */ 
  199.  public function deleltById($id,$tableName){ 
  200.   $affact = false; 
  201.   $sql = 'delete from '.trim($tableName).' where id = '.$id; 
  202.   try { 
  203.    $conn = $this->getConnection(); 
  204.    $conn->exec($sql); 
  205.    $this->closeConnection($conn); 
  206.    $affact = true; 
  207.   } catch (PDOException  $e) { 
  208.    print 'Delelte error is '.$e->getMessage(); 
  209.   } 
  210.   return $affact; 
  211.  } 
  212.   
  213.  /** 
  214.   * 功能:      以and 的形式删除记录 
  215.   * @param $tableName    表的名称 
  216.   * @param $array        数组表中字段名=其值的方式进行组合 
  217.   */ 
  218.  public  function prepareDeleteAnd($tableName,array $array=null){ 
  219.   $sql = 'delete from '. $tableName . ' where '; 
  220.   $count = count($array);//计算数组的长度 
  221.   $flag = 0;//设置标记 
  222.  
  223.   foreach ($array as $key => $value){ 
  224.    $flag++;//让flag增加一 
  225.    $sql .= $key .'='."'".$value."'"; 
  226.    if ($flag != $count ){//当falg不等于count时,数组还有值,后面增加and,反之不增加 
  227.     $sql .= ' and '; 
  228.    } 
  229.   } 
  230.   echo  $sql;//测试sql语句的组合   
  231.   try { 
  232.    $conn = $this->getConnection();//获取连接 
  233.    $conn->prepare($sql); 
  234.    $this->closeConnection(); 
  235.   } catch (PDOException $e) { 
  236.    print 'Delete error is '.$e->getMessage(); 
  237.   } 
  238.    
  239.  } 
  240.   
  241.  
  242.  /** 
  243.   * 功能:         以or 的形式删除记录 
  244.   * @param $tableName    表的名称 
  245.   * @param $array        数组表中字段名=其值的方式进行组合 
  246.   */ 
  247.  public  function prepareDeleteOr($tableName,array $array=null){ 
  248.   
  249.   $sql = 'delete from '. $tableName . ' where '; 
  250.   $count = count($array);//计算数组的长度 
  251.   $flag = 0;//设置标记 
  252.  
  253.   foreach ($array as $key => $value){ 
  254.    $flag++;//让flag增加一 
  255.    $sql .= $key .'='."'".$value."'"; 
  256.    if ($flag != $count ){//当falg不等于count时,数组还有值,后面增加and,反之不增加 
  257.     $sql .= ' or '; 
  258.    } 
  259.   } 
  260.   echo  $sql;//测试sql语句的组合   
  261.   try { 
  262.    $conn = $this->getConnection();//获取连接 
  263.    $stmt = $conn->prepare($sql); 
  264.    $stmt->execute();//执行 
  265.    $this->closeConnection(); 
  266.   } catch (PDOException $e) { 
  267.    print 'Delete error is '.$e->getMessage(); 
  268.   } 
  269.    
  270.  } 
  271.   
  272.   
  273.  /** 
  274.   * 功能:      取得表中所有数据 
  275.   * @param  $sql     sql语句 
  276.   */ 
  277.  public function getAll($sql){ 
  278.    
  279.   $result = null; 
  280.   try { 
  281.    $conn = $this->getConnection(); 
  282.    $result = $conn->query($sql); 
  283.    $this->closeConnection($conn); 
  284.   } catch (PDOException $e) { 
  285.    print 'GetAll error is '.$e->getMessage(); 
  286.   } 
  287.  } 
  288.   
  289.   
  290.  /** 
  291.   * 功能:更新数据表中的信息 
  292.   * @param  $table      要更新的表名 
  293.   * @param array $updateFiled    要更新的字段 
  294.   * @param array $updateConditon 更新需要的条件 
  295.   */ 
  296.  public function updateDataBase($table,array $updateFiled,array $updateConditon ){ 
  297.     
  298.   $sql   = 'update from ' .$table .' set '; 
  299.    
  300.   //对set字段进行赋值操作 
  301.   $count = count($updateFiled);//获取要修改数组的长度 
  302.   $flag  = 0;//设置标记为0 
  303.   foreach ($updateFiled as $key => $value){ 
  304.    $flag++; 
  305.    $sql .= $key .'='."'".$value."'"; 
  306.    if ($flag != $count){ 
  307.     $sql .=','; 
  308.    } 
  309.   } 
  310.   //对where条件进行赋值 
  311.   $countUpdateCondition = count($updateConditon);//获取要修改数组的长度 
  312.   $flag  = 0;//设置标记为0 
  313.   $sql .= ' where '; 
  314.   foreach ($updateConditon as $key => $value){ 
  315.    $flag++; 
  316.    $sql .= $key .'='."'".$value."'"; 
  317.    if ($flag != $countUpdateCondition){ 
  318.     $sql .=' and '; 
  319.    } 
  320.   } 
  321.   try { 
  322.    $conn = $this->getConnection(); 
  323.    $conn->exec($sql); 
  324.    $this->closeConnection($conn); 
  325.   } catch (PDOException $e) { 
  326.    print 'Update error is :'.$e->getMessage(); 
  327.   } 
  328.    
  329.  } 
  330.   
  331.   
  332.  /** 
  333.   * 功能:      根据表和提高的查询条件进行查询 
  334.   * 返回值:      返回结果集 
  335.   * @param  $table    数据表名 
  336.   * @param array $findCondition  查询条件 
  337.   */ 
  338.  public function findData($table,array $findCondition){ 
  339.    
  340.   $sql = 'select from '.$table .' where '; 
  341.    
  342.   $count = count($findCondition);//获取查询条件数组的长度 
  343.   $flag  = 0;//设置标记为0 
  344.   foreach ($findCondition as $key => $value){ 
  345.    $flag++; 
  346.    $sql .= $key .'='."'".$value."'"; 
  347.    if ($flag != $count){ 
  348.     $sql .=' and '; 
  349.    } 
  350.   } 
  351.   try { 
  352.     $conn = $this->getConnection(); 
  353.     $conn->exec($sql); 
  354.     $this->closeConnection($conn); 
  355.    } catch (PDOException $e) { 
  356.     print 'find error is :'.$e->getMessage(); 
  357.    } 
  358.     
  359.   } 
  360. } 
  361. //测试 
  362.  
  363. $db = new PdoMysqlOperater(); 
  364. $db->findData('liujijun',array('name'=>'liujijun','name1'=>'liujijun')); 
  365. ?>
  366.  



出处:http://www.phpfensi.com/php/20140911/5337.html


相关教程