/upload/system/library/driver/database/mpdo.php

https://github.com/daudmabena/opencart · PHP · 105 lines · 91 code · 14 blank · 0 comment · 10 complexity · 1c93a239caa276fe1da94fd33c1be28c MD5 · raw file

  1. <?php
  2. final class DBmPDO {
  3. private $pdo = null;
  4. private $statement = null;
  5. public function __construct($hostname, $username, $password, $database, $port = "3306") {
  6. try {
  7. $this->pdo = new PDO("mysql:host=".$hostname.";port=".$port.";dbname=".$database, $username, $password, array(PDO::ATTR_PERSISTENT => true));
  8. } catch(PDOException $e) {
  9. trigger_error('Error: Could not make a database link ( '. $e->getMessage() . '). Error Code : ' . $e->getCode() . ' <br />');
  10. }
  11. $this->pdo->exec("SET NAMES 'utf8'");
  12. $this->pdo->exec("SET CHARACTER SET utf8");
  13. $this->pdo->exec("SET CHARACTER_SET_CONNECTION=utf8");
  14. $this->pdo->exec("SET SQL_MODE = ''");
  15. }
  16. public function prepare($sql) {
  17. $this->statement = $this->pdo->prepare($sql);
  18. }
  19. public function bindParam($parameter, $variable, $data_type = PDO::PARAM_STR, $length = 0) {
  20. if ($length) {
  21. $this->statement->bindParam($parameter, $variable, $data_type, $length);
  22. } else {
  23. $this->statement->bindParam($parameter, $variable, $data_type);
  24. }
  25. }
  26. public function execute() {
  27. try {
  28. if ($this->statement && $this->statement->execute()) {
  29. $data = array();
  30. while ($row = $this->statement->fetch(PDO::FETCH_ASSOC)) {
  31. $data[] = $row;
  32. }
  33. $result = new stdClass();
  34. $result->row = ( isset($data[0]) ? $data[0] : array() );
  35. $result->rows = $data;
  36. $result->num_rows = $this->statement->rowCount();
  37. }
  38. } catch(PDOException $e) {
  39. trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
  40. }
  41. }
  42. public function query($sql, $params = array()) {
  43. $this->statement = $this->pdo->prepare($sql);
  44. $result = false;
  45. try {
  46. if ($this->statement && $this->statement->execute($params)) {
  47. $data = array();
  48. while ($row = $this->statement->fetch(PDO::FETCH_ASSOC)) {
  49. $data[] = $row;
  50. }
  51. $result = new stdClass();
  52. $result->row = (isset($data[0]) ? $data[0] : array());
  53. $result->rows = $data;
  54. $result->num_rows = $this->statement->rowCount();
  55. }
  56. } catch (PDOException $e) {
  57. trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
  58. exit();
  59. }
  60. if ($result) {
  61. return $result;
  62. } else {
  63. $result = new stdClass();
  64. $result->row = array();
  65. $result->rows = array();
  66. $result->num_rows = 0;
  67. return $result;
  68. }
  69. }
  70. public function escape($value) {
  71. $search = array("\\", "\0", "\n", "\r", "\x1a", "'", '"');
  72. $replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"');
  73. return str_replace($search, $replace, $value);
  74. }
  75. public function countAffected() {
  76. if ($this->statement) {
  77. return $this->statement->rowCount();
  78. } else {
  79. return 0;
  80. }
  81. }
  82. public function getLastId() {
  83. return $this->pdo->lastInsertId();
  84. }
  85. public function __destruct() {
  86. $this->pdo = null;
  87. }
  88. }