当前位置:首页 > Mysql教程 > 列表

mysql PDO 操作类的例子

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-25 13:55:47 浏览: 评论:0 

下面来给各位同这介绍一个mysql PDO 操作类例子,希望些文章对大家会带来帮助,文章是转朋友的自己没写.

介绍:1、只有在执行select、update、delete、insert等操作时才会连接数据库,2、采用PDO预处理方式,3、事务处理,4、错误输出.

php代码如下:

  1. <?php 
  2.  
  3. /** 
  4.  * mysql PDO 操作类 
  5.  * Created by PhpStorm. 
  6.  * User: sumiaowen 
  7.  * Date: 14-3-12 
  8.  * Time: 下午4:57 
  9.  * To change this template use File | Settings | File Templates. 
  10.  */ 
  11. class MyMysql 
  12. { 
  13.  //pdo 链接 mysql dns 
  14.  static $dns = null; 
  15.  
  16.  //mysql 用户名 
  17.  static $username = null; 
  18.  
  19.  //mysql 密码 
  20.  static $password = null; 
  21.  
  22.  //pdo 链接实例 
  23.  static $pdo = null; 
  24.  
  25.  //调试 
  26.  public $debug = null; 
  27.  
  28.  //开始事务 
  29.  private $_begin_transaction = false; 
  30.  
  31.  /** 
  32.   * @param bool   $debug    是否开启调试,错误信息输出 
  33.   * @param string $database 数据库类别 
  34.   */ 
  35.  public function __construct($debug = true, $database = 'default') 
  36.  { 
  37.   $this->debug    = $debug; 
  38.   self::$dns      = Yaf_Registry::get('config')->db->$database->dns; 
  39.   self::$username = Yaf_Registry::get('config')->db->$database->username; 
  40.   self::$password = Yaf_Registry::get('config')->db->$database->password; 
  41.  } 
  42.  
  43.  /** 
  44.   * PDO对象实例化 
  45.   * @return null|PDO 
  46.   */ 
  47.  static function instance() 
  48.  { 
  49.   if(is_null(self::$pdo)) 
  50.   { 
  51.    try 
  52.    { 
  53.     self::$pdo = new PDO(self::$dns, self::$username, self::$password); 
  54.     self::$pdo->query('set names utf8'); 
  55.    } 
  56.    catch(PDOException $e) 
  57.    { 
  58.     exit('PDOException: ' . $e->getMessage()); 
  59.    } 
  60.   } 
  61.  
  62.   return self::$pdo; 
  63.  } 
  64.  
  65.  /** 
  66.   * 预处理执行 select sql语句 
  67.   * @param string $sql 
  68.   * @param array  $parameters 
  69.   * @param int    $option 
  70.   * @return array 
  71.   */ 
  72.  public function query($sql, $parameters = array(), $option = PDO::FETCH_ASSOC) 
  73.  { 
  74.   self::$pdo || self::instance(); 
  75.  
  76.   $stmt = self::$pdo->prepare($sql); 
  77.   $stmt->execute($parameters); 
  78.  
  79.   $tmp = array(); 
  80.   while($row = $stmt->fetch($option)) 
  81.   { 
  82.    $tmp[] = $row; 
  83.   } 
  84.  
  85.   if($this->debug) 
  86.   { 
  87.    $this->error($stmt); 
  88.   } 
  89.  
  90.   return $tmp; 
  91.  } 
  92.  
  93.  /** 
  94.   * 预处理执行 update、delete、insert SQL语句 
  95.   * @param sting $sql 
  96.   * @param array $parameters 
  97.   * @return int 返回影响行数 
  98.   */ 
  99.  public function execute($sql, $parameters = array()) 
  100.  { 
  101.   self::$pdo || self::instance(); 
  102.  
  103.   $stmt = self::$pdo->prepare($sql); 
  104.   $stmt->execute($parameters); 
  105.  
  106.   if($this->debug) 
  107.   { 
  108.    $this->error($stmt); 
  109.   } 
  110.  
  111.   return $stmt->rowCount(); 
  112.  } 
  113.  
  114.  /** 
  115.   * 执行一条SQL语句 
  116.   * @param string $sql 
  117.   * @return int 返回影响行数 
  118.   */ 
  119.  public function exec($sql) 
  120.  { 
  121.   self::$pdo || self::instance(); 
  122.  
  123.   $rows = self::$pdo->exec($sql); 
  124.  
  125.   if($this->debug) 
  126.   { 
  127.    $this->error(); 
  128.   } 
  129.  
  130.   return $rows; 
  131.  } 
  132.  
  133.  /** 
  134.   * 添加一条记录 
  135.   * @param string $tableName 数据库表名 
  136.   * @param array  $data      需要添加的数据,一个 key|value 对应的数组,其中key为表字段名称,value为插入的值,如:$data = array('keyword'=>'关键词') 
  137.   * @return int 返回插入行的ID 
  138.   */ 
  139.  public function insert($tableName, $data) 
  140.  { 
  141.   self::$pdo || self::instance(); 
  142.  
  143.   $fields = '`' . implode('`,`', array_keys($data)) . '`'; 
  144.  
  145.   $values = "'" . implode("','", $data) . "'"; 
  146.  
  147.   $sql = "INSERT INTO `{$tableName}`({$fields}) VALUES ({$values})"; 
  148.  
  149.   self::$pdo->exec($sql); 
  150.  
  151.   if($this->debug) 
  152.   { 
  153.    $this->error(); 
  154.   } 
  155.  
  156.   return $this->getLastInsertId(); 
  157.  } 
  158.  
  159.  /** 
  160.   * 添加多条数据 
  161.   * @param string $tableName 数据库表名 
  162.   * @param array  $data      需要添加的数据,为一个二维数组,如:$data = array(array('fileld1'=>'value1','fileld2'=>'value2'),array('fileld1'=>'value1','fileld2'=>'value2')) 
  163.   * @return int 返回影响行数 
  164.   */ 
  165.  public function insertBatch($tableName, $data) 
  166.  { 
  167.   self::$pdo || self::instance(); 
  168.  
  169.   $fields = '`' . implode('`,`', array_keys($data[0])) . '`'; 
  170.  
  171.   $tmp = array(); 
  172.   foreach($data as $value) 
  173.   { 
  174.    $tmp[] = "'" . implode("','", $value) . "'"; 
  175.   } 
  176.  
  177.   $values = "(" . implode("),(", $tmp) . ")"; 
  178.  
  179.   $sql = "INSERT INTO `{$tableName}`({$fields}) VALUES {$values}"; 
  180.  
  181.   $rows = self::$pdo->exec($sql); 
  182.  
  183.   if($this->debug) 
  184.   { 
  185.    $this->error(); 
  186.   } 
  187.  
  188.   return $rows; 
  189.  } 
  190.  
  191.  /** 
  192.   * 根据主键更新数据 
  193.   * @param string $tableName 数据库表名 
  194.   * @param array  $where     更新条件,为 key|value 对应的数组,如:array('id'=>233) 
  195.   * @param array  $data      更新数据,为 key|value 对应的数组,如:array('field1'=>'value1','field12'=>'value2') 
  196.   * @return int 成功返回影响行数,失败返回错误信息 
  197.   */ 
  198.  public function updateByPrimaryKey($tableName, $where, $data) 
  199.  { 
  200.   self::$pdo || self::instance(); 
  201.  
  202.   //条件 
  203.   $whereId    = array_keys($where); 
  204.   $whereValue = array_values($where); 
  205.  
  206.   $tmp = array(); 
  207.   foreach($data as $key => $value) 
  208.   { 
  209.    $tmp[] = "`{$key}`='{$value}'"; 
  210.   } 
  211.  
  212.   $data = implode(',', $tmp); 
  213.  
  214.   $sql = "UPDATE `{$tableName}` SET {$data} WHERE `{$whereId[0]}`='{$whereValue[0]}'"; 
  215.  
  216.   $rows = self::$pdo->exec($sql); 
  217.  
  218.   if($this->debug) 
  219.   { 
  220.    $this->error(); 
  221.   } 
  222.  
  223.   return $rows; 
  224.  } 
  225.  
  226.  /** 
  227.   * 根据主键删除数据 
  228.   * @param string $tableName 数据库表名 
  229.   * @param array  $where     删除条件,为 key|value 对应的数组,如:array('id'=>233) 
  230.   * @return int 成功返回影响行数,失败返回错误信息 
  231.   */ 
  232.  public function deleteByPrimaryKey($tableName, $where) 
  233.  { 
  234.   self::$pdo || self::instance(); 
  235.  
  236.   //条件 
  237.   $whereId    = array_keys($where); 
  238.   $whereValue = array_values($where); 
  239.  
  240.   $sql = "DELETE FROM `{$tableName}` WHERE `{$whereId[0]}`='{$whereValue[0]}'"; 
  241.  
  242.   $rows = self::$pdo->exec($sql); 
  243.  
  244.   if($this->debug) 
  245.   { 
  246.    $this->error(); 
  247.   } 
  248.  
  249.   return $rows; 
  250.  } 
  251.  
  252.  /** 
  253.   * 返回最后插入行的ID或序列值 
  254.   * @return int 
  255.   */ 
  256.  public function getLastInsertId() 
  257.  { 
  258.   self::$pdo || self::instance(); 
  259.  
  260.   return self::$pdo->lastInsertId(); 
  261.  } 
  262.  
  263.  /** 
  264.   * 设置错误信息 
  265.   */ 
  266.  public function error($stmt = '') 
  267.  { 
  268.   $error = $stmt ? $stmt->errorInfo() : self::$pdo->errorInfo(); 
  269.  
  270.   $msg = "SQLSTATE:{$error[0]}"; 
  271.  
  272.   if($error[1]) 
  273.   { 
  274.    $msg .= " - ERRORCODE:{$error[1]}"; 
  275.   } 
  276.  
  277.   if($error[2]) 
  278.   { 
  279.    $msg .= " - ERROR:{$error[2]}"; 
  280.   } 
  281.  
  282.   if($error[1] || $error[2]) 
  283.   { 
  284.    exit($msg); 
  285.   } 
  286.  } 
  287.  
  288.  /** 
  289.   * 事务开始 
  290.   * @return bool 
  291.   */ 
  292.  public function begin() 
  293.  { 
  294.   self::$pdo || self::instance(); 
  295.  
  296.   //已经有事务,退出事务 
  297.   $this->rollback(); 
  298.  
  299.   if(!self::$pdo->beginTransaction()) 
  300.   { 
  301.    return false; 
  302.   } 
  303.  
  304.   return $this->_begin_transaction = true; 
  305.  } 
  306.  
  307.  /** 
  308.   * 事务提交 
  309.   * @return bool 
  310.   */ 
  311.  public function commit() 
  312.  { 
  313.   if($this->_begin_transaction) 
  314.   { 
  315.    $this->_begin_transaction = false; 
  316.    self::$pdo->commit(); 
  317.   } 
  318.  
  319.   return true; 
  320.  } 
  321.  
  322.  /** 
  323.   * 事务回滚 
  324.   * @return bool 
  325.   */ 
  326.  public function rollback() 
  327.  { 
  328.   if($this->_begin_transaction) 
  329.   { 
  330.    $this->_begin_transaction = false; 
  331.    self::$pdo->rollback(); 
  332.   }//开源代码phpfensi.com 
  333.  
  334.   return false; 
  335.  } 
  336.  
  337.  /** 
  338.   * 关闭链接 
  339.   */ 
  340.  public function close() 
  341.  { 
  342.   self::$pdo = null; 
  343.  } 
  344. } 
  345. ?>

Tags: mysql操作类 PDO操作类

分享到: