I'm trying to wrap my head around OOP, and am fairly new to PHP in general, so please forgive me if the following makes you cringe.
I've decided to try and create a basic database wrapper using PDO. Here's what I've done so far.
class DB {
protected $db_user = 'root';
protected $db_pass = '';
protected $db_host = 'localhost';
protected $db_name = 'login';
private $dbh;
private $sth;
public function __construct() {
$this->dbh = new PDO('mysql:host='.$this->db_host.'; dbname='.$this->db_name, $this->db_user, $this->db_pass);
}
public function select($get, $table, $where){
$this->sth = $this->dbh->prepare('SELECT '.$get.' FROM '.$table.' WHERE '.$where);
}
public function bindSTR($name, $replacement) {
$this->sth->bindParam($name, $replacement, PDO::PARAM_STR);
}
public function execute(){
$this->sth->execute();
}
public function fetch(){
return $this->sth->fetch(PDO::FETCH_ASSOC);
}
}
And here is the usage:
$DB = new DB();
$DB->select('user_id, email', 'users', 'email = :email');
$DB->bindSTR(':email', 'examp[email protected]');
$DB->execute();
$results = $DB->fetch();
Now before I go ahead and start adding more functionality to the class, can anyone give me some feedback?
Is this a good way to go? How can I improve the class? Any suggestions in general?