下面来给各位同这介绍一个mysql PDO 操作类例子,希望些文章对大家会带来帮助,文章是转朋友的自己没写.,介绍:1、只有在执行select、update、delete、insert等操作时才会连接数据库,2、采用PDO预处理方式,3、事务处理,4、错误输出.,php代码如下:,

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

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

php代码如下:

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

,

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

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

php代码如下:

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