<?php
namespace DB;
final class PDO {
private $connection;
private $statement;
// 构造方法
public function __construct($hostname, $username, $password, $database, $port = '3306') {
try {
// 实例化
$this->connection = @new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true));
} catch (\PDOException $e) {
throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname . '!');
}
$this->connection->exec("SET NAMES 'utf8'");
$this->connection->exec("SET CHARACTER SET utf8");
$this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8");
$this->connection->exec("SET SQL_MODE = ''");
}
// 执行
public function execute() {
try {
if ($this->statement && $this->statement->execute()) {
$data = array();
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
$data[] = $row;
}
$result = new \stdClass();
$result->row = (isset($data[0])) ? $data[0] : array();
$result->rows = $data;
$result->num_rows = $this->statement->rowCount();
}
} catch (\PDOException $e) {
throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode());
}
}
// 查询
public function query($sql, $params = array()) {
$this->statement = $this->connection->prepare($sql);
$result = false;
try {
if ($this->statement && $this->statement->execute($params)) {
$data = array();
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
$data[] = $row;
}
$result = new \stdClass();
$result->row = (isset($data[0]) ? $data[0] : array());
$result->rows = $data;
$result->num_rows = $this->statement->rowCount();
}
} catch (\PDOException $e) {
throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql);
}
if ($result) {
return $result;
} else {
$result = new \stdClass();
$result->row = array();
$result->rows = array();
$result->num_rows = 0;
return $result;
}
}
// 预处理
public function prepare($sql) {
$this->statement = $this->connection->prepare($sql);
}
// 绑定参数
public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) {
if ($length) {
$this->statement->bindParam($parameter, $variable, $data_type, $length);
} else {
$this->statement->bindParam($parameter, $variable, $data_type);
}
}
// 过滤
public function escape($value) {
return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value);
}
// 受影响行数
public function countAffected() {
if ($this->statement) {
return $this->statement->rowCount();
} else {
return 0;
}
}
// 插入最后一条记录的ID
public function getLastId() {
return $this->connection->lastInsertId();
}
// 判断是否已经连接
public function isConnected() {
if ($this->connection) {
return true;
} else {
return false;
}
}
// 关闭连接
public function __destruct() {
$this->connection = null;
}
}