pdoClass.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. } catch (PDOException $e) {
  14. $this->conn = null;
  15. $this->errormsg = $e->getMessage();
  16. }
  17. }
  18. protected function querysql($sql)
  19. {
  20. try {
  21. $bo = $this->conn->query($sql);
  22. } catch (PDOException $e) {
  23. $bo = false;
  24. $this->setError($e->getMessage(), $sql);
  25. }
  26. return $bo;
  27. }
  28. public function fetch_array($result, $type = 0)
  29. {
  30. $result_type = ($type==0)? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
  31. return $result->fetch($result_type);
  32. }
  33. public function insert_id()
  34. {
  35. return $this->conn->lastInsertId();
  36. }
  37. protected function starttran()
  38. {
  39. $this->conn->beginTransaction();
  40. }
  41. protected function endtran($bo)
  42. {
  43. if(!$bo){
  44. $this->conn->rollBack();
  45. }else{
  46. $this->conn->commit();
  47. }
  48. }
  49. public function close()
  50. {
  51. if($this->conn==null)return;
  52. return $this->conn=null;
  53. }
  54. }
粤ICP备19079148号