大佬教程收集整理的这篇文章主要介绍了php封装的数据库函数与用法示例【参考thinkPHP】,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了php封装的数据库函数与用法。分享给大家供大家参考,具体如下:
从Thinkphp里面抽离出来的数据库模块,感觉挺好用
common.php:
php;">
php
/
**
* 通用函数
*/
//包含配置文件
if (is_file("config.
php"))
{
C(include 'config.
php'
);
}
if (!function_exists("__autoload"))
{
function __autoload($class_
Name) {
require_once('classes/' . $class_name . '.clas
s.php'
);
}
}
/
**
* 数据库操作函数
* @return \mysqli
*/
function M()
{
$db = new Model(
);
if (mysqli_connect_errno())
throw_exception(mysqli_connect_error()
);
return $db;
}
// 获取配置值
function C($name = null,$value = null)
{
//静态全局变量,
后面的使用取值都是在 $)config数组取
static $_config = array(
);
// 无参数时获取所有
if (empty($
Name))
return $_config;
// 优先执行设置获取或赋值
if (is_
String($
Name))
{
if (!strpos($name,'.'))
{
$name = strtolower($
Name);
if (is_null($
value))
return isset($_config[$name]) ? $_config[$name] : null;
$_config[$name] = $value;
return;
}
// 二维数组设置和获取支持
$name = explode('.',$
Name);
$name
[0] = strtolower($name
[0]);
if (is_null($
value))
return isset($_config[$name
[0]][$name[1]]) ? $_config[$name
[0]][$name[1]] : null;
$_config[$name
[0]][$name[1]] = $value;
return;
}
// 批量设置
if (is_array($
Name))
{
return $_config = array_merge($_config,array_change_key_case($
Name));
}
return null; // 避免非法参数
}
function ajaxReturn($data = null,$
message = "",$status)
{
$ret = array(
);
$ret["data"] = $data;
$ret["
message"] = $
message;
$ret["status"] = $status;
echo json_encode($ret
);
die(
);
}
//调试数组
function _dump($var)
{
if (C("debug"))
dump($var
);
}
// 浏览器友好的变量输出
function dump($var,$echo = true,$label = null,$
Strict = tru
E) {
$label = ($label === null) ? '' : rtrim($label) . ' ';
if (!$
Strict)
{
if (ini_get('html_errors'))
{
$output = print_r($var,tru
E);
$output = '
' . $label . htmlspecialchars($output,ENT_QUOTES) . '
';
} else
{
$output = $label . print_r($var,tru
E);
}
} 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;
}
/
**
* 调试输出
* @param type $msg
*/
function _debug($msg)
{
if (C("debug"))
echo "$msg
";
}
function _log($fil
ename,$msg)
{
$time = date("Y-m-d H:i:s"
);
$msg = "[$time]\n$msg\r\n";
if (C("log"))
{
$fd = fopen($fil
ename,"a+"
);
fwrite($fd,$msg
);
fclose($fd
);
}
}
/
**
* 日志记录
* @param type $str
*/
function L($msg)
{
$time = date("Y-m-d H:i:s"
);
$clientIP = $_SERVER['REMOTE_ADDR'];
$msg = "[$time $clientIP] $msg\r\n";
$log_file = C("LOGFILE"
);
_log($log_file,$msg
);
}
?>
config.php:
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/目录下:
php;">
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(
);
}
}
/
**
* 获得所有的查询数据
* @access private
* @param
String $sql
sql语句
* @return array
*/
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;
}
/
**
* 获取一条记录的某个字段值,sql 由自己组织
* 例子: $model->getField("
SELEct id from user limit 1")
*/
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(
);
}
}
/
**
* 执行语句 , 例
如插入,更新操作
* @access public
* @param
String $str sql指令
* @return
Integer
*/
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->las
TinsID = $this->db->insert_id;
return $this->numRows;
}
}
/
**
* 获得所有的查询数据
* @access private
* @param
String $sql
sql语句
* @return array
*/
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;
}
/
**
* 返回最后插入的ID
*/
public function getLas
TinsID()
{
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(
);
}
}
例子:
query($sql
);
_dump($list
);
}
更多关于php相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《@L_801_3@》
希望本文所述对大家php程序设计有所帮助。
大佬总结
以上是大佬教程为你收集整理的php封装的数据库函数与用法示例【参考thinkPHP】全部内容,希望文章能够帮你解决php封装的数据库函数与用法示例【参考thinkPHP】所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。