PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Database.class.php

https://bitbucket.org/PHPSocial/phpsocial
PHP | 247 lines | 96 code | 42 blank | 109 comment | 13 complexity | 80c073450b4a615eed78286424935005 MD5 | raw file
  1. <?php
  2. /**
  3. * Database class.
  4. *
  5. */
  6. class PHPS_Database {
  7. /**
  8. * database link identifier
  9. *
  10. * @var mysql
  11. */
  12. public $database_connection;
  13. /**
  14. * Query logged flag
  15. *
  16. * @var boolean
  17. */
  18. public $log_stats;
  19. /**
  20. * Query run info
  21. *
  22. * @var array
  23. */
  24. public $query_stats;
  25. function __construct($database_host, $database_username, $database_password, $database_name) {
  26. $this->database_connection = $this->database_connect($database_host, $database_username, $database_password);
  27. $this->database_select($database_name);
  28. $this->log_stats = 1;
  29. $this->query_stats = array();
  30. }
  31. /**
  32. * Connect to database
  33. *
  34. * @param string $database_host
  35. * @param string $database_username
  36. * @param string $database_password
  37. * @return mysql_connection link
  38. */
  39. public function database_connect($database_host, $database_username, $database_password) {
  40. if ($c = mysql_connect($database_host, $database_username, $database_password, TRUE)) {
  41. return $c;
  42. } else {
  43. if (file_exists(dirname(__FILE__) . "/../Install.php")) {
  44. header("location: Install.php"); die();
  45. } else {
  46. echo "Database connection error"; die();
  47. }
  48. }
  49. }
  50. /**
  51. * select a db
  52. *
  53. * @param string $database_name
  54. * @return mysql link
  55. */
  56. public function database_select($database_name) {
  57. return mysql_select_db($database_name, $this->database_connection);
  58. }
  59. /**
  60. * Make query to the database
  61. *
  62. * @param string $database_query
  63. * @return mixed
  64. */
  65. public function database_query($database_query) {
  66. $query_timer_start = getmicrotime();
  67. $query_result = mysql_query($database_query, $this->database_connection);
  68. if($this->log_stats != 0) {
  69. $query_time = round(getmicrotime()-$query_timer_start, 5);
  70. $this->query_stats[] = Array('query' => $database_query,
  71. 'time' => $query_time);
  72. }
  73. return $query_result;
  74. }
  75. /**
  76. * Executes query and returns query result
  77. *
  78. * @param string $query SQL query text
  79. * bool $get_errors (optional) show SQL errors? FALSE by default
  80. * @access public
  81. * @return bool
  82. */
  83. public function get_one($query) {
  84. if (!($res = mysql_query($query)) and $this->get_errors){
  85. echo "Database error: ".mysql_error()."<br/>In query: ".$query;
  86. }
  87. $row = @mysql_fetch_row($res);
  88. $keys = @array_keys($row);
  89. return $row[$keys[0]];
  90. }
  91. /**
  92. * Executes query and returns query result (row, array)
  93. *
  94. * @param string $query SQL query text
  95. * bool $get_errors (optional) show SQL errors? FALSE by default
  96. * @access public
  97. * @return array
  98. */
  99. public function get_row($query) {
  100. if (!strstr(strtoupper($query), "LIMIT")) $query .= " LIMIT 0,1";
  101. if (!($res = mysql_query($query)) and $this->get_errors){
  102. echo "Database error: ".mysql_error()."<br/>In query: ".$query;
  103. }
  104. return @mysql_fetch_assoc($res);
  105. }
  106. /**
  107. * Executes query and returns query result (table, array of array)
  108. *
  109. * @param string $query SQL query text
  110. * bool $get_errors (optional) show SQL errors? FALSE by default
  111. * @access public
  112. * @return bool
  113. */
  114. public function get_all($query) {
  115. if (!($res = mysql_query($query)) and $this->get_errors){
  116. echo "Database error: ".mysql_error()."<br/>In query: ".$query;
  117. }
  118. while ($result[] = @mysql_fetch_assoc($res)) {}
  119. $result = array_slice($result, 0, count($result)-1);
  120. return $result;
  121. }
  122. /**
  123. * Executes query and returns query result (column, array)
  124. *
  125. * @param string $query SQL query text
  126. * bool $get_errors (optional) show SQL errors? FALSE by default
  127. * @access public
  128. * @return bool
  129. */
  130. public function get_col($query) {
  131. if (!($res = mysql_query($query)) and $this->get_errors){
  132. echo "Database error: ".mysql_error()."<br/>In query: ".$query;
  133. }
  134. while ($table[] = @mysql_fetch_assoc($res)) {}
  135. $table = array_slice($table, 0, count($table)-1);
  136. $result = array();
  137. foreach ($table as $row){
  138. $keys = array_keys($row);
  139. $key = $keys[0];
  140. unset($keys);
  141. $result[] = $row[$key];
  142. }
  143. return $result;
  144. }
  145. /**
  146. * Fetch row as numeric array
  147. *
  148. * @param mixed $database_result
  149. * @return array
  150. */
  151. public function database_fetch_array($database_result) {
  152. return mysql_fetch_array($database_result);
  153. }
  154. /**
  155. * Fetch row as associative array
  156. *
  157. * @param mixed $database_result
  158. * @return array
  159. */
  160. function database_fetch_assoc($database_result) {
  161. return mysql_fetch_assoc($database_result);
  162. }
  163. /**
  164. * Returns number of rows in the result
  165. *
  166. * @param mixed $database_result
  167. * @return integer
  168. */
  169. function database_num_rows($database_result) {
  170. return mysql_num_rows($database_result);
  171. }
  172. /**
  173. * Returns number of rows in the result
  174. *
  175. * @param mixed $database_result
  176. * @return integer
  177. */
  178. function database_affected_rows($database_result) {
  179. return mysql_affected_rows($database_result);
  180. }
  181. /**
  182. * Returns the last inserted id
  183. *
  184. * @return integer
  185. */
  186. function database_insert_id() {
  187. return mysql_insert_id($this->database_connection);
  188. }
  189. /**
  190. * Get db error
  191. *
  192. * @return string
  193. */
  194. function database_error() {
  195. return mysql_error($this->database_connection);
  196. }
  197. /**
  198. * Close db connection
  199. *
  200. */
  201. function database_close() {
  202. mysql_close($this->database_connection);
  203. }
  204. }
  205. ?>