1
0

pdoClass.php 1.3 KB

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