Guest User

Untitled

a guest
Feb 9th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. <?php
  2. class Database {
  3. protected $_link;
  4. protected $_result;
  5. protected $_numRows;
  6. private $_host = "HOST";
  7. private $_username = "DATABASE USERNAME";
  8. private $_password = "DATABASE PASSWORD";
  9. private $_database = "DATABASE";
  10.  
  11. // Establish connection to database, when class is instantiated
  12. public function __construct() {
  13. $this->_link = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
  14. if(mysqli_connect_errno()) {
  15. echo "Connection Failed: " . mysqli_connect_errno();
  16. exit();
  17. }
  18. }
  19.  
  20. // Sends the query to the connection
  21. public function Query($sql) {
  22. $this->_result = $this->_link->query($sql) or die(mysqli_error($this->_result));
  23. $this->_numRows = mysqli_num_rows($this->_result);
  24. }
  25.  
  26. // Inserts into databse
  27. public function UpdateDb($sql) {
  28. $this->_result = $this->_link->query($sql) or die(mysqli_error($this->_result));
  29. return $this->_result;
  30. }
  31.  
  32. // Return the number of rows
  33. public function NumRows() {
  34. return $this->_numRows;
  35. }
  36.  
  37. // Fetchs the rows and return them
  38. public function Rows() {
  39. $rows = array();
  40.  
  41. for($x = 0; $x < $this->NumRows(); $x++) {
  42. $rows[] = mysqli_fetch_assoc($this->_result);
  43. }
  44. return $rows;
  45. }
  46.  
  47. // Used by other classes to get the connection
  48. public function GetLink() {
  49. return $this->_link;
  50. }
  51.  
  52. // Securing input data
  53. public function SecureInput($value) {
  54. return mysqli_real_escape_string($this->_link, $value);
  55. }
  56. }
  57. ?>
Add Comment
Please, Sign In to add comment