本文实例讲述了php封装的数据库函数与用法。分享给大家供大家参考,具体如下:
从Thinkphp里面抽离出来的数据库模块,感觉挺好用
common.php:
' . $label . htmlspecialchars($output, ENT_QUOTES) . ''; } else { $output = $label . print_r($var, true); } } else { ob_start(); var_dump($var); $output = ob_get_clean(); if (!extension_loaded('xdebug')) { $output = preg_replace("/]=>n(s+)/m", '] => ', $output); $output = '
' . $label . htmlspecialchars($output, ENT_QUOTES) . ''; } } if ($echo) { echo($output); return null; } else return $output; } function _debug($msg) { if (C("debug")) echo "$msg
"; } function _log($filename, $msg) { $time = date("Y-m-d H:i:s"); $msg = "[$time]n$msgrn"; if (C("log")) { $fd = fopen($filename, "a+"); fwrite($fd, $msg); fclose($fd); } } function L($msg) { $time = date("Y-m-d H:i:s"); $clientIP = $_SERVER['REMOTE_ADDR']; $msg = "[$time $clientIP] $msgrn"; $log_file = C("LOGFILE"); _log($log_file, $msg); } ?>
config.php:
'mysql', 'DB_HOST' => '127.0.0.1', 'DB_NAME' => 'DB', 'DB_USER' => 'USER', 'DB_PWD' => 'PWD', 'DB_PORT' => '3306', ); return $db; ?>
数据库模型类Model.class.php,放到classes/目录下:
db = $this->connect();
}
public function connect($config = '', $linkNum = 0) {
if (!isset($this->linkID[$linkNum])) {
if (empty($config))
$config = array(
'username' => C('DB_USER'),
'password' => C('DB_PWD'),
'hostname' => C('DB_HOST'),
'hostport' => C('DB_PORT'),
'database' => C('DB_NAME')
);
$this->linkID[$linkNum] = new mysqli($config['hostname'], $config['username'], $config['password'], $config['database'], $config['hostport'] ? intval($config['hostport']) : 3306);
if (mysqli_connect_errno())
throw_exception(mysqli_connect_error());
$this->connected = true;
}
return $this->linkID[$linkNum];
}
protected function initConnect() {
if (!$this->connected) {
$this->db = $this->connect();
}
}
public function select($sql) {
$this->initConnect();
if (!$this->db)
return false;
$query = $this->db->query($sql);
$list = array();
if (!$query)
return $list;
while ($rows = $query->fetch_assoc()) {
$list[] = $rows;
}
return $list;
}
public function find($sql) {
$resultSet = $this->select($sql);
if (false === $resultSet) {
return false;
}
if (empty($resultSet)) {// 查询结果为空
return null;
}
$data = $resultSet[0];
return $data;
}
public function getField($sql) {
$resultSet = $this->select($sql);
if (!empty($resultSet)) {
return reset($resultSet[0]);
}
}
public function query($str) {
$this->initConnect();
if (!$this->db) {
if (C("debug"))
echo "connect to database error";
return false;
}
$this->queryStr = $str;
//释放前次的查询结果
if ($this->queryID)
$this->free();
$this->queryID = $this->db->query($str);
// 对存储过程改进
if ($this->db->more_results()) {
while (($res = $this->db->next_result()) != NULL) {
$res->free_result();
}
}
//$this->debug();
if (false === $this->queryID) {
echo $this->error();
return false;
} else {
$this->numRows = $this->queryID->num_rows;
$this->numCols = $this->queryID->field_count;
return $this->getAll();
}
}
public function execute($str) {
$this->initConnect();
if (!$this->db)
return false;
$this->queryStr = $str;
//释放前次的查询结果
if ($this->queryID)
$this->free();
$result = $this->db->query($str);
if (false === $result) {
$this->error();
return false;
} else {
$this->numRows = $this->db->affected_rows;
$this->lastInsID = $this->db->insert_id;
return $this->numRows;
}
}
private function getAll() {
//返回数据集
$result = array();
if ($this->numRows > 0) {
//返回数据集
for ($i = 0; $i < $this->numRows; $i++) {
$result[$i] = $this->queryID->fetch_assoc();
}
$this->queryID->data_seek(0);
}
return $result;
}
public function getLastInsID() {
return $this->db->insert_id;
}
// 返回最后执行的sql语句
public function _sql() {
return $this->queryStr;
}
public function error() {
$this->error = $this->db->errno . ':' . $this->db->error;
if ('' != $this->queryStr) {
$this->error .= "n [ SQL语句 ] : " . $this->queryStr;
}
//trace($this->error, '', 'ERR');
return $this->error;
}
public function free() {
$this->queryID->free_result();
$this->queryID = null;
}
public function close() {
if ($this->db) {
$this->db->close();
}
$this->db = null;
}
public function __destruct() {
if ($this->queryID) {
$this->free();
}
// 关闭连接
$this->close();
}
}
例子:
#include "common.php"
function test(){
$model = M();
$sql = "select * from test";
$list = $model->query($sql);
_dump($list);
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。



