PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/mysql.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 70 lines | 54 code | 16 blank | 0 comment | 7 complexity | e28e1d6d9606aa0f6dfbcbf0ec537919 MD5 | raw file
  1. <?php
  2. final class MySQL {
  3. private $connection;
  4. public function __construct($hostname, $username, $password, $database) {
  5. if (!$this->connection = mysql_connect($hostname, $username, $password)) {
  6. exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
  7. }
  8. if (!mysql_select_db($database, $this->connection)) {
  9. exit('Error: Could not connect to database ' . $database);
  10. }
  11. mysql_query("SET NAMES 'utf8'", $this->connection);
  12. mysql_query("SET CHARACTER SET utf8", $this->connection);
  13. mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection);
  14. mysql_query("SET SQL_MODE = ''", $this->connection);
  15. }
  16. public function query($sql) {
  17. $resource = mysql_query($sql, $this->connection);
  18. if ($resource) {
  19. if (is_resource($resource)) {
  20. $i = 0;
  21. $data = array();
  22. while ($result = mysql_fetch_assoc($resource)) {
  23. $data[$i] = $result;
  24. $i++;
  25. }
  26. mysql_free_result($resource);
  27. $query = new stdClass();
  28. $query->row = isset($data[0]) ? $data[0] : array();
  29. $query->rows = $data;
  30. $query->num_rows = $i;
  31. unset($data);
  32. return $query;
  33. } else {
  34. return true;
  35. }
  36. } else {
  37. trigger_error('Error: ' . mysql_error($this->connection) . '<br />Error No: ' . mysql_errno($this->connection) . '<br />' . $sql);
  38. exit();
  39. }
  40. }
  41. public function escape($value) {
  42. return mysql_real_escape_string($value, $this->connection);
  43. }
  44. public function countAffected() {
  45. return mysql_affected_rows($this->connection);
  46. }
  47. public function getLastId() {
  48. return mysql_insert_id($this->connection);
  49. }
  50. public function __destruct() {
  51. mysql_close($this->connection);
  52. }
  53. }
  54. ?>