Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Database {
- private $_connection;
- private $_host = "localhost";
- private $_username = "root";
- private $_password = "";
- private $_database = "cmsi";
- // Conect to database is private and can only be used by getConection function result is returned object of mysqli class
- //can be seen in var_dump() function
- private function conect() {
- //$this refers to class Database and her functions and propertys are being acsessed via -> sign
- $this->_connection = new mysqli($this->_host, $this->_username,
- $this->_password, $this->_database);
- // Error handling
- if(mysqli_connect_error()) {
- trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
- E_USER_ERROR);
- }
- return $this->_connection;
- }
- //Safest way to iniate conection
- public function getConection(){
- return $this -> conect();
- }
- }
- class Category{
- private $db_sql;
- public function __construct($db){
- $this->db_sql = $db;
- }
- public function read(){
- $conection = $this->db_sql;
- $query = "Select * from category";
- $result = $conection -> query($query);
- return $result;
- }
- }
- $db = new Database();
- $conection = $db->getConection();
- $obj = new Category($conection);
- $obj->read();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement