Advertisement
Guest User

Untitled

a guest
Jan 14th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. class Database {
  2. private $_connection;
  3. private $_host = "localhost";
  4. private $_username = "root";
  5. private $_password = "";
  6. private $_database = "cmsi";
  7.  
  8. // Conect to database is private and can only be used by getConection function result is returned object of mysqli class
  9. //can be seen in var_dump() function
  10. private function conect() {
  11. //$this refers to class Database and her functions and propertys are being acsessed via -> sign
  12. $this->_connection = new mysqli($this->_host, $this->_username,
  13. $this->_password, $this->_database);
  14.  
  15. // Error handling
  16. if(mysqli_connect_error()) {
  17. trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
  18. E_USER_ERROR);
  19. }
  20. return $this->_connection;
  21.  
  22. }
  23. //Safest way to iniate conection
  24. public function getConection(){
  25. return $this -> conect();
  26. }
  27. }
  28.  
  29.  
  30. class Category{
  31. private $db_sql;
  32. public function __construct($db){
  33. $this->db_sql = $db;
  34. }
  35.  
  36. public function read(){
  37. $conection = $this->db_sql;
  38. $query = "Select * from category";
  39. $result = $conection -> query($query);
  40. return $result;
  41. }
  42.  
  43. }
  44.  
  45. $db = new Database();
  46. $conection = $db->getConection();
  47. $obj = new Category($conection);
  48. $obj->read();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement