I'm just learning PHP and am new to OOP as well. Reading a few books on it and some show mysqli but I understand it's got a procedural method and an OOP method. I try to implement the OOP method in a class but it doesn't work.
<?php
class DB {
protected $host = '127.0.0.1:3306';
protected $dbname = 'users';
protected $username = 'user';
protected $password = 'pass';
public function connect() {
$mysqli = new mysqli($this->host, $this->username, $this->password, $this->dbname);
return true;
}
public function select($table, $where) {
global $mysqli;
$query = "SELECT * FROM $table WHERE $where";
$result = $mysqli->query($query);
if($result->num_rows == 1)
return $this->processRowSet($result, true);
return $this->processRowSet($result);
}
}
?>
And the object:
<?php
//include 'db.class.php';
include 'database_login.php';
$db = new DB();
$db->connect();
$db->select('users.users','id=1');
?>
The error:
Fatal error: Call to a member function query() on a non-object in /Library/WebServer/Documents/tutorial/database_login.php on line 17
Any help would be much appreciated. Maybe talking slowly and using little words, I guess.
If it's needed.. PHP version: 5.5.14