Guest User

Untitled

a guest
Jul 12th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. ## index.php
  2.  
  3. // Includes
  4. require_once("includes/class_database.php");
  5.  
  6. // Variables
  7. $fooBar = '111';
  8. $database = new database();
  9.  
  10. // Grab Database
  11. $query = "SELECT * FROM table WHERE foo_bar = '$fooBar'";
  12. $result = $database->database_query($query);
  13. $count = $database->count_rows($result);
  14.  
  15. if($count == 0)
  16. {
  17. echo 'No results';
  18. echo $result;
  19. }
  20.  
  21. else
  22. {
  23. // ...more code
  24. }
  25.  
  26. ## includes/class_database.php
  27.  
  28. <?
  29. /*
  30.  
  31. THIS CLASS CONTAINS DATABASE RELATED FUNCTIONS
  32.  
  33. FUNCTIONS USED IN THIS CLASS:
  34.  
  35. init() Connects to the server and selects the database
  36.  
  37. database_connect() Connects to the database
  38. database_select() Selects the database
  39. database_query() Queries the database
  40. database_error() Returns the database error
  41. database_close() Closes the connection to the database
  42.  
  43. count_rows() Returns the amount of rows in a result
  44.  
  45. */
  46.  
  47. class database
  48. {
  49. private $db_host = 'localhost';
  50. private $db_user = 'root';
  51. private $db_pass = '';
  52. private $db_name = 'diwa';
  53.  
  54. //private $database_connection = $this->database_connect($db_host,$db_user,$db_pass);
  55.  
  56. //
  57. // CONNECTS TO THE SERVER AND SELECTS THE DATABASE
  58. //
  59.  
  60. function init()
  61. {
  62. $this->database_connection = $this->database_connect($this->db_host, $this->db_user, $this->db_pass) or die($this->database_error());
  63. $this->database_select($this->db_name) or die($this->database_error());
  64.  
  65. // This will prevent some problems on MySQL5+ servers
  66. mysql_query("SET sql_mode='MYSQL40'", $this->database_connection);
  67. }
  68.  
  69. // END init()
  70.  
  71.  
  72.  
  73.  
  74.  
  75. //
  76. // CONNECT TO THE DATABASE
  77. //
  78.  
  79. function database_connect($db_host, $db_user, $db_pass)
  80. {
  81. return mysql_connect($this->host, $this->user, $this->pass, TRUE);
  82. }
  83.  
  84. // END db_connect()
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. //
  92. // THIS METHOD SELECTS A DATABASE
  93. //
  94.  
  95. function database_select($database_name)
  96. {
  97. return mysql_select_db($this->db_name, $this->database_connection);
  98. }
  99.  
  100. // END database_select()
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. //
  108. // THIS METHOD QUERIES A DATABASE
  109. //
  110.  
  111. function database_query($query)
  112. {
  113. $result = mysql_query($query, $this->database_connection);
  114. return $result;
  115. }
  116.  
  117. // END db_query()
  118.  
  119.  
  120.  
  121.  
  122. //
  123. // THIS METHOD RETURNS THE DATABASE ERROR
  124. //
  125.  
  126. function database_error()
  127. {
  128. return mysql_error($this->database_connection);
  129. }
  130.  
  131. // END database_error()
  132.  
  133.  
  134.  
  135.  
  136.  
  137. //
  138. // THIS METHOD CLOSES THE CONNECTION TO THE DATABASE SERVER
  139. //
  140.  
  141. function database_close()
  142. {
  143. return mysql_close($this->database_connection);
  144. }
  145.  
  146. // END database_close()
  147.  
  148.  
  149.  
  150.  
  151.  
  152. //
  153. // THIS METHOD RETURNS THE NUMBER OF ROWS IN A RESULT
  154. //
  155.  
  156. function count_rows($result)
  157. {
  158. if( !is_resource($result) ) return FALSE;
  159. return mysql_num_rows($result);
  160. }
  161.  
  162. // END database_row_count()
  163.  
  164.  
  165.  
  166.  
  167.  
  168. }
  169. ?>
Add Comment
Please, Sign In to add comment