pdoClass.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. include_once('mysql.php');
  3. class pdoClass extends mysql{
  4. protected function connect()
  5. {
  6. $this->errormsg = '';
  7. if(!class_exists('PDO'))exit('操作数据库的php的扩展PDO不存在');
  8. try {
  9. $this->conn = @new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_base.'', $this->db_user, $this->db_pass);
  10. $this->conn->query("SET NAMES 'utf8'");
  11. $this->selectdb($this->db_base);
  12. } catch (PDOException $e) {
  13. $this->conn = null;
  14. $this->errormsg = $e->getMessage();
  15. }
  16. }
  17. protected function querysql($sql)
  18. {
  19. try {
  20. $bo = $this->conn->query($sql);
  21. } catch (PDOException $e) {
  22. $bo = false;
  23. $this->errormsg = $e->getMessage();
  24. }
  25. return $bo;
  26. }
  27. public function fetch_array($result, $type = 0)
  28. {
  29. $result_type = ($type==0)? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
  30. return $result->fetch($result_type);
  31. }
  32. public function insert_id()
  33. {
  34. return $this->conn->lastInsertId();
  35. }
  36. protected function starttran()
  37. {
  38. $this->conn->beginTransaction();
  39. }
  40. protected function endtran($bo)
  41. {
  42. if(!$bo){
  43. $this->conn->rollBack();
  44. }else{
  45. $this->conn->commit();
  46. }
  47. }
  48. public function error()
  49. {
  50. $str = $this->conn->errorInfo();
  51. return 'pdoError('.$str[0].'):'.$str[2].''.$this->errormsg.'';
  52. }
  53. public function close()
  54. {
  55. if($this->conn==null)return;
  56. return $this->conn=null;
  57. }
  58. }
粤ICP备19079148号