我正在编写一个简单的 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);
}
}