mpdo.php (3080B)
1 <?php 2 namespace DB; 3 final class mPDO { 4 private $connection = null; 5 private $statement = null; 6 7 public function __construct($hostname, $username, $password, $database, $port = '3306') { 8 try { 9 $this->connection = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true)); 10 } catch(\PDOException $e) { 11 throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); 12 } 13 14 $this->connection->exec("SET NAMES 'utf8'"); 15 $this->connection->exec("SET CHARACTER SET utf8"); 16 $this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8"); 17 $this->connection->exec("SET SQL_MODE = ''"); 18 } 19 20 public function prepare($sql) { 21 $this->statement = $this->connection->prepare($sql); 22 } 23 24 public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) { 25 if ($length) { 26 $this->statement->bindParam($parameter, $variable, $data_type, $length); 27 } else { 28 $this->statement->bindParam($parameter, $variable, $data_type); 29 } 30 } 31 32 public function execute() { 33 try { 34 if ($this->statement && $this->statement->execute()) { 35 $data = array(); 36 37 while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { 38 $data[] = $row; 39 } 40 41 $result = new \stdClass(); 42 $result->row = (isset($data[0])) ? $data[0] : array(); 43 $result->rows = $data; 44 $result->num_rows = $this->statement->rowCount(); 45 } 46 } catch(\PDOException $e) { 47 throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); 48 } 49 } 50 51 public function query($sql, $params = array()) { 52 $this->statement = $this->connection->prepare($sql); 53 54 $result = false; 55 56 try { 57 if ($this->statement && $this->statement->execute($params)) { 58 $data = array(); 59 60 while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { 61 $data[] = $row; 62 } 63 64 $result = new \stdClass(); 65 $result->row = (isset($data[0]) ? $data[0] : array()); 66 $result->rows = $data; 67 $result->num_rows = $this->statement->rowCount(); 68 } 69 } catch (\PDOException $e) { 70 throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql); 71 } 72 73 if ($result) { 74 return $result; 75 } else { 76 $result = new \stdClass(); 77 $result->row = array(); 78 $result->rows = array(); 79 $result->num_rows = 0; 80 return $result; 81 } 82 } 83 84 public function escape($value) { 85 return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value); 86 } 87 88 public function countAffected() { 89 if ($this->statement) { 90 return $this->statement->rowCount(); 91 } else { 92 return 0; 93 } 94 } 95 96 public function getLastId() { 97 return $this->connection->lastInsertId(); 98 } 99 100 public function isConnected() { 101 if ($this->connection) { 102 return true; 103 } else { 104 return false; 105 } 106 } 107 108 public function __destruct() { 109 $this->connection = null; 110 } 111 }