Guest User

Untitled

a guest
Jun 5th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. <?php
  2. /**
  3. * Database class.
  4. * Class to interact with the database for the program.
  5. */
  6. class Database {
  7.  
  8. ##########################################################
  9.  
  10. /* Database Details */
  11. private $database_server; // database server, provided by user.
  12. private $database_username; // database username, provided by user.
  13. private $database_password; // database password, provided by user, can be blank.
  14. private $database_database; // database name, provided by user
  15.  
  16. /* Program Cache */
  17. private $query; // stores the last query resource here.
  18. private $error; // stores the program error here. get_error() retrieves it for public use.
  19.  
  20. ##########################################################
  21.  
  22. /**
  23. * init function.
  24. * Inits the class, attempts to connect and select the database.
  25. * @access public
  26. * @param array $database_details
  27. * @return bool
  28. */
  29. public function init($database_details) {
  30.  
  31. /* Checks to see if the data we got was in the form of an array. */
  32. if(!is_array($database_details)) {
  33. $this->error = "Database details must be in the form of an array.";
  34. return false;
  35. }
  36.  
  37. /* Checks to see if any values were empty.. other then password.. */
  38. if(empty($database_details["server"]) || empty($database_details["username"]) || empty($database_details["database"])) {
  39. $this->error = "One of the required database details was left blank.";
  40. return false;
  41. }
  42.  
  43. /* Lets set the details now to the class */
  44. $this->database_server = $database_details["server"];
  45. $this->database_username = $database_details["username"];
  46. $this->database_password = $database_details["password"];
  47. $this->database_database = $database_details["database"];
  48.  
  49. /* Now we need to try and connect to the MySQL server, if it isn't good, we stop here. */
  50. if(!$this->connect_mysql()) {
  51. return false;
  52. }
  53.  
  54. /* Now we need to try and select the database from the MySQL server, if it doesn't turn back good, stop. */
  55. if(!$this->select_database()) {
  56. return false;
  57. }
  58.  
  59. // passed.
  60. return true;
  61. }
  62.  
  63.  
  64. ########################
  65. # Connection Functions #
  66. ########################
  67.  
  68. /**
  69. * connect_mysql function.
  70. * Attempts to connect to MySQL with user provided details.
  71. * @access private
  72. * @return bools
  73. */
  74. private function connect_mysql() {
  75. /* This below will try and connect to the MySQL server with the details. */
  76. $connnect_resource = @mysql_connect($this->database_server, $this->database_username, $this->database_password);
  77.  
  78. /* If it didn't work, we need to get the error from MySQL, and stop */
  79. if(!$connect_resource) {
  80. $this->error = mysql_error();
  81. return false;
  82. }
  83.  
  84. return true; // it seems to have worked!
  85. }
  86.  
  87. /**
  88. * select_db function.
  89. * Attempts to select a database from the MySQL server with the provided details.
  90. * @access private
  91. * @return bool
  92. */
  93. private function select_db() {
  94. /* This will try and select the database from the MySQL server. */
  95. $select_resource = @mysql_select_db($this->database_database);
  96.  
  97. /* If it didn't go successfully, we need to get the error from MySQL and stop here. */
  98. if(!$select_resource) {
  99. $this->error = mysql_error();
  100. return false;
  101. }
  102.  
  103. return true; // selected it successfully! :)
  104. }
  105.  
  106. ###################
  107. # Query functions #
  108. ###################
  109.  
  110. /**
  111. * query function.
  112. * Executes a provided query on the MySQL server and stores and returns it.
  113. * @access public
  114. * @param string $query_text
  115. * @return mysql_query_resource
  116. */
  117. public function query($query_text) {
  118.  
  119. /* We must have some sort of query to run.. checks it. */
  120. if(empty($query_text)) {
  121. $this->error = "Query must have some text to execute";
  122. return false;
  123. }
  124.  
  125. $query = @mysql_query($query_text); // execute the query.
  126. $this->query = $query; // store the resource in this class.
  127.  
  128. return $query; // return the resource.
  129. }
  130.  
  131. /**
  132. * num_rows function.
  133. * Numbers the rows returned on a given query.
  134. * @access public
  135. * @param mysql_query_resource $query_resource. (default: '')
  136. * @return mysql_num_rows_resource
  137. */
  138. public function num_rows($query_resource = '') {
  139. $query_resource = ($query_resource = '') ? $this->query : $query_resource; // if the query resource is blank, use last query.
  140.  
  141. $num_rows = @mysql_num_rows($query_resource); // execute it..
  142.  
  143. return $num_rows; // return the num_rows resource.
  144. }
  145.  
  146. /**
  147. * fetch_object function.
  148. * Fetch's an object with the data from a query
  149. * @access public
  150. * @param mysql_query_resource $query_resource
  151. * @return mysql_object_resource
  152. */
  153. public function fetch_object($query_resource = '') {
  154. $query_resource = ($query_resource = '') ? $this->query : $query_resource; // if the query resource is blank, use last query.
  155.  
  156. $fetch_object = @mysql_fetch_object($query_resource); // fetch it..
  157.  
  158. return $fetch_object; // return the object.
  159. }
  160.  
  161. /**
  162. * fetch_array function.
  163. * Fetch's an array with the data from a query.
  164. * @access public
  165. * @param mysql_query_resource $query_resource. (default: '')
  166. * @return mysql_array_resource
  167. */
  168. public function fetch_array($query_resource = '') {
  169. $query_resource = ($query_resource = '') ? $this->query : $query_resource; // if the query resource is blank, use last query.
  170.  
  171. $fetch_array = @mysql_fetch_array($query_resource); // fetch it..
  172.  
  173. return $fetch_array; // return the array.
  174. }
  175.  
  176. /**
  177. * count_table function.
  178. * Counts up all the rows in a table. Faster then just mysql_num_rows() the table.
  179. * @access public
  180. * @param string $table_name
  181. * @return int
  182. */
  183. public function count_table($table_name) {
  184.  
  185. /* Checks to make sure the table name is not empty. */
  186. if(empty($table_name)) {
  187. $this->error = "You need a table name to count!"; // sets the program error.
  188. return false;
  189. }
  190.  
  191. $this->query("SELECT COUNT(*) FROM `$table_name`"); // execute the count query.. faster then just numing all the rows.
  192. $count = $this->mysql_fetch_array(); // gets the array for the count.
  193.  
  194. return $count[0]; // return the count.
  195. }
  196.  
  197. ###################
  198. # Class Functions #
  199. ###################
  200.  
  201. /**
  202. * get_error function.
  203. * Get's the error from the class, if any.
  204. * @access public
  205. * @return string
  206. */
  207. public function get_error() {
  208. return $this->error;
  209. }
  210. }
Add Comment
Please, Sign In to add comment