<?php
//custom PDO class
class MyPdo {
//db host
private $_dbh;
private $_stmt;
private $_queryCounter = 0;
//constructor
public function __construct($user, $pass, $dbname) {
$dsn = 'mysql:host=localhost;dbname=' . $dbname;
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::ATTR_PERSISTENT => true);
try {
$this->_dbh = new PDO($dsn, $user, $pass, $dbname, $options);
} catch (Exception $exc) {
echo $exc->getMessage();
}
}
//query method
public function prepare($query) {
$this->_stmt = $this->_dbh->prepare($query);
}
public function bind($pos, $value, $type = null) {
switch (true) {
$type = PDO::PARAM_INT();
break;
$type = PDO::PARAM_BOOL();
break;
$type = PDO::PARAM_NULL();
break;
default :
$type = PDO::PARAM_STR();
}
}
$this->_stmt->bindValue($pos, $value, $type);
}
//execute method
public function execute() {
$this->_queryCounter++;
return $this->_stmt->execute();
}
// begin transaction // must be innoDatabase table
public function beginTransaction() {
return $this->_dbh->beginTransaction();
}
//fetch all
public function fetchAll() {
$this->execute();
return $this->_stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function fetch() {
$this->execute();
return $this->_stmt->fetch(PDO::FETCH_ASSOC);
}
// returns last insert ID
//!!!! if called inside a transaction, must call it before closing the transaction!!!!!!
public function lastInsertId() {
return $this->_dbh->lastInsertId();
}
// returns number of queries executed
public function queryCounter() {
return $this->_queryCounter;
}
// cancel transaction
public function cancelTransaction() {
return $this->_dbh->rollBack();
}
// returns number of rows updated, deleted, or inserted
public function rowCount() {
return $this->_stmt->rowCount();
}
}
?>