Guest User

Untitled

a guest
Mar 13th, 2019
52
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.  
  15. if(mysqli_connect_errno()) {
  16. echo "Connection Failed: " . mysqli_connect_errno();
  17. exit();
  18. }
  19. }
  20.  
  21. // Sends the query to the connection
  22. public function Query($sql) {
  23. $this->_result = $this->_link->query($sql) or die(mysqli_error($this->_result));
  24. $this->_numRows = mysqli_num_rows($this->_result);
  25. }
  26.  
  27. // Inserts into databse
  28. public function UpdateDb($sql) {
  29. $this->_result = $this->_link->query($sql) or die(mysqli_error($this->_result));
  30. return $this->_result;
  31. }
  32.  
  33. // Return the number of rows
  34. public function NumRows() {
  35. return $this->_numRows;
  36. }
  37.  
  38. // Fetchs the rows and return them
  39. public function Rows() {
  40. $rows = array();
  41.  
  42. for($x = 0; $x < $this->NumRows(); $x++) {
  43. $rows[] = mysqli_fetch_assoc($this->_result);
  44. }
  45. return $rows;
  46. }
  47.  
  48. // Used by other classes to get the connection
  49. public function GetLink() {
  50. return $this->_link;
  51. }
  52.  
  53. // Securing input data
  54. public function SecureInput($value) {
  55. return mysqli_real_escape_string($this->_link, $value);
  56. }
  57. }
  58. ?>
Add Comment
Please, Sign In to add comment