Location: PHPKode > projects > Monyet > includes/dbaccess.php

//singleton pattern for DBAccess

Class DBAccess {
	private $dbres;
	private $hostname;
	private $dbname;
	private $user;
	private $pass;

	static private $instance = null;

	//private function can be accessed from DBAccess::getInstance
	private function __construct(){
		$this->hostname = 'localhost';
		$this->dbname = 'monyet';
		$this->user = 'root';
		$this->pass = '';
		$this->dbres = new PDO('mysql:host='.$this->hostname.';dbname='.$this->dbname, $this->user, $this->pass,array(
		     PDO::ATTR_PERSISTENT => true));
		$this->dbres->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	private function __clone(){}

	public static function getInstance(){
		if(self::$instance == null){
			self::$instance = new DBAccess();
		return self::$instance;

	//command query return array of object if any, or array of single object
	public function query($sql){
		$stmt = $this->dbres->prepare($sql);	
		$array = $stmt->fetchAll(PDO::FETCH_OBJ);
		return $array;

	//just execute the sql and return true. for update or remove
	public function exec($sql){
		$stmt = $this->dbres->prepare($sql);	
		return true;	

	//release all resource 
	public function __destruct(){
		$this->hostname = null;
		$this->dbname = null;
		$this->user = null;
		$this->pass = null;
		$this->dbres = null;
Return current item: Monyet