PageRenderTime 31ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/Web Files/ASP/includes/db/class.database.php

https://code.google.com/p/bf2stats/
PHP | 194 lines | 138 code | 22 blank | 34 comment | 17 complexity | d1e5c9c598c367a2c1bc28c91032098b MD5 | raw file
  1. <?php
  2. /****************************************************************************/
  3. /* < Database Class For BF2statistics > */
  4. /* Written By Wilson212 */
  5. /****************************************************************************/
  6. class Database
  7. {
  8. // Queries statistics.
  9. var $_statistics = array(
  10. 'time' => 0,
  11. 'count' => 0,
  12. );
  13. private $mysql;
  14. /************************************************************
  15. * Creates the connection to the mysql database, selects the posted DB
  16. * Returns 0 if unable to connect to the database
  17. * Returns 2 if the Database does not exist
  18. * Returns TRUE on success
  19. */
  20. public function __construct($db_host, $db_port, $db_user, $db_pass, $db_name)
  21. {
  22. $this->mysql = @mysql_connect($db_host.":".$db_port, $db_user, $db_pass, true);
  23. $this->selected_database = @mysql_select_db($db_name, $this->mysql);
  24. return TRUE;
  25. }
  26. // ************************************************************
  27. // Closes the mysql DB connection
  28. public function __destruct()
  29. {
  30. @mysql_close($this->mysql) or die(mysql_error());
  31. }
  32. /************************************************************
  33. * Checks the connection to the mysql database, selects the posted DB
  34. * Returns 0 if unable to connect to the database
  35. * Returns 2 if the Database does not exist
  36. * Returns TRUE on success
  37. */
  38. public function status()
  39. {
  40. if(!$this->mysql)
  41. {
  42. return 0;
  43. }
  44. if(!$this->selected_database)
  45. {
  46. return 2;
  47. }
  48. return 1;
  49. }
  50. // ************************************************************
  51. // Query function is best used for INSERT and UPDATE functions
  52. public function query($query)
  53. {
  54. $sql = mysql_query($query,$this->mysql) or die("Couldnt Run Query: ".$query."<br />Error: ".mysql_error($this->mysql)."");
  55. $this->_statistics['count']++;
  56. return TRUE;
  57. }
  58. // ************************************************************
  59. // Select function is great for getting huge arrays of multiple rows and tables
  60. public function select($query)
  61. {
  62. $sql = mysql_query($query,$this->mysql) or die("Couldnt Run Query: ".$query."<br />Error: ".mysql_error($this->mysql)."");
  63. $this->_statistics['count']++;
  64. $i = 1;
  65. if(mysql_num_rows($sql) == 0)
  66. {
  67. $result = FALSE;
  68. }
  69. else
  70. {
  71. while($row = mysql_fetch_assoc($sql))
  72. {
  73. foreach($row as $colname => $value)
  74. {
  75. $result[$i][$colname] = $value;
  76. }
  77. $i++;
  78. }
  79. }
  80. return $result;
  81. }
  82. // ************************************************************
  83. // selectRow is perfect for getting 1 row of data. Technically can be used for multiple rows,
  84. // though select function is better for more then 1 row
  85. public function selectRow($query)
  86. {
  87. $sql = mysql_query($query,$this->mysql) or die("Couldnt Run Query: ".$query."<br />Error: ".mysql_error($this->mysql)."");
  88. $this->_statistics['count']++;
  89. if(mysql_num_rows($sql) == 0)
  90. {
  91. return FALSE;
  92. }
  93. else
  94. {
  95. $row = mysql_fetch_array($sql);
  96. return $row;
  97. }
  98. }
  99. // ************************************************************
  100. // selectCell returns 1 cell of data, Not recomended unless you want data from a specific cell in a table
  101. public function selectCell($query)
  102. {
  103. $sql = mysql_query($query,$this->mysql) or die("Couldnt Run Query: ".$query."<br />Error: ".mysql_error($this->mysql)."");
  104. $this->_statistics['count']++;
  105. if(mysql_num_rows($sql) == 0)
  106. {
  107. return FALSE;
  108. }
  109. else
  110. {
  111. $row = mysql_fetch_array($sql);
  112. return $row['0'];
  113. }
  114. }
  115. // ************************************************************
  116. // count is a perfect function for counting the num of rows, or results in a table
  117. // returns the direct count, for ex: 5
  118. public function count($query)
  119. {
  120. $sql = mysql_query($query, $this->mysql) or die("Couldnt Run Query: ".$query."<br />Error: ".mysql_error($this->mysql)."");
  121. $this->_statistics['count']++;
  122. return mysql_result($sql, 0);
  123. }
  124. // ************************************************************
  125. // Run a sql file function. Not written by me.
  126. // $file is the path location to the sql file
  127. function runSQL($file)
  128. {
  129. $handle = @fopen($file, "r");
  130. if ($handle)
  131. {
  132. while(!feof($handle))
  133. {
  134. $sql_line[] = fgets($handle);
  135. }
  136. fclose($handle);
  137. }
  138. else
  139. {
  140. return FALSE;
  141. }
  142. foreach ($sql_line as $key => $query)
  143. {
  144. if (trim($query) == "" || strpos ($query, "--") === 0 || strpos ($query, "#") === 0)
  145. {
  146. unset($sql_line[$key]);
  147. }
  148. }
  149. unset($key, $query);
  150. foreach ($sql_line as $key => $query)
  151. {
  152. $query = rtrim($query);
  153. $compare = rtrim($query, ";");
  154. if ($compare != $query)
  155. {
  156. $sql_line[$key] = $compare . "|br3ak|";
  157. }
  158. }
  159. unset($key, $query);
  160. $sql_lines = implode($sql_line);
  161. $sql_line = explode("|br3ak|", $sql_lines);
  162. foreach($sql_line as $query)
  163. {
  164. if($query)
  165. {
  166. mysql_query($query, $this->mysql) or die("Couldnt Run Query: ".$query."<br />Error: ".mysql_error($this->mysql)."");
  167. }
  168. }
  169. return TRUE;
  170. }
  171. }
  172. ?>