Просмотр файла engine/classes/mysqli.class.php

Размер файла: 3.12Kb
  1. <?php
  2.  
  3. /**
  4. * @author Tadochi
  5. */
  6.  
  7. class sql
  8. {
  9. public $queries = 0;
  10. public $db;
  11. public $error;
  12. public $result = null; // Результат последнего запроса
  13. public $timer;
  14.  
  15. function __construct($set)
  16. {
  17. $time_before = microtime(1);
  18. $this->db = mysqli_connect($set['mysql_host'], $set['mysql_user'], $set['mysql_pass'], $set['mysql_db_name']);
  19. mysqli_set_charset($this->db, 'utf8');//mysqli_query($this->db, 'SET NAMES utf8');
  20. $this->timer += microtime(1) - $time_before;
  21. return $this->db;
  22. }
  23.  
  24. function __destruct()
  25. {
  26. $this->free();
  27. mysqli_close($this->db);
  28. }
  29. public function free($multi = false)
  30. {
  31. if ($multi)
  32. {
  33. do
  34. {
  35. if (!mysqli_more_results($this->db))
  36. break;
  37.  
  38. if ($result = mysqli_store_result($this->db))
  39. {
  40. mysqli_free_result($result);
  41. }
  42. }
  43. while (mysqli_next_result($this->db));
  44. }
  45.  
  46. if (!empty($this->result) && !is_bool($this->result))
  47. mysqli_free_result($this->result);
  48. $this->result = null;
  49. }
  50.  
  51. public function num_rows($query = false)
  52. {
  53. return mysqli_affected_rows($this->db);
  54. }
  55.  
  56. public function result($query = false)
  57. {
  58. if (!$query)
  59. $query = $this->result;
  60. $result = mysqli_fetch_row($query);
  61. return $result[0];
  62. }
  63. public function fetch($query = false)
  64. {
  65. if (!$query)
  66. $query = $this->result;
  67. return mysqli_fetch_assoc($query);
  68. }
  69.  
  70. public function from_file($file)
  71. {
  72. if (file_exists($file))
  73. return $this->multi(file_get_contents($file));
  74. }
  75.  
  76. public function multi($query)
  77. {
  78. $time_before = microtime(1);
  79. $this->result = mysqli_multi_query($this->db, $query);
  80. $this->queries += substr_count($query, ';');
  81. $this->timer += microtime(1) - $time_before;
  82. $this->error = mysqli_error($this->db);
  83. return $this->result;
  84. }
  85. public function query($query, $free = false)
  86. {
  87. $time_before = microtime(1);
  88. $result = mysqli_query($this->db, $query);
  89. $this->timer += microtime(1) - $time_before;
  90. $this->error = mysqli_error($this->db);
  91.  
  92. if ($free)
  93. mysqli_free_result($result);
  94. else
  95. $this->result = $result;
  96.  
  97. if (!empty($this->error))
  98. {
  99. mysqli_query($this->db, "
  100. INSERT INTO `errors`
  101. (`user`, `time`, `user_agent`, `ip`, `desc`, `type`, `url`) VALUES
  102. ('".Core::$user_id."', '".time()."', '".my_esc($_SERVER['HTTP_USER_AGENT'], true)."', '".my_esc($_SERVER['REMOTE_ADDR'], true)."', '".my_esc($this->error)."', 'mysql', '".my_esc($_SERVER['REQUEST_URI'], true)."');");
  103. $this->queries++;
  104. $this->error = null;
  105. }
  106.  
  107. $this->queries++;
  108.  
  109. return $this;
  110. }
  111. }
  112. return new sql($set);