単純な PDO データベース クラスを作成していて、結果を自動的にバインドしたいのですが、どのようにすればよいかわかりません。
クラスをできるだけシンプルにしたいので、他の方法を知っていれば、このクラスをシンプルにすることができます.
これが私のデータベースクラスです:
class Database {
private $connect;
private $query;
private $stmt;
/**
* @ Connect to the database and set PDO error mode
*/
public function __construct() {
try {
$this->connect = new PDO("mysql:host=localhost;dbname=blog", "root", "root");
$this->connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $error) {
echo "ERROR: " . $error->getMessage();
}
}
/**
* @ Perpare the database query
*/
public function query($query) {
$this->stmt = $this->connect->prepare($query);
if ($action == 'insert' || $action == 'update') {
reset ($array);
}
}
/**
* @ Bind the results
*
*/
public function bind() {
// THIS IS WHERE I NEED HELP
}
/**
* @ Execute the query
*/
public function execute() {
return $this->stmt->execute();
}
/**
* @ Return a set of results
*/
public function results() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
/**
* @ Return a single result
*/
public function single() {
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}