php中怎么把数据库连接写成一个接口我自己封装的一个
?php
class AppConfig{
public static $dbParam = array(
'dbHost' = 'localhost',
'dbUser' = 'root',
'dbPassword' ='',
'dbName' = '数据库名',
'dbCharset' = 'utf8',
'dbPort' = 3306,
'dbPrefix' = 'test_',
'dbPconnect' = 0,
'dbDebug' = true,
);
}
class Model {
private$version = '';//mysql版本
private$config = array();//数据库配置数组
private$class;//当前类名
public$tablepre = 'ts_';//表前缀
public$db = '';//库名
public$table = '';//表名
privatestatic $link;//数据库链接句柄
private$data = https://www.04ip.com/post/array();//中间数据容器
private$condition= '';//查询条件
private$fields = array();//字段信息
private$sql = array();//sql集合,调试用
public$primaryKey = 'id';//表主键
//构造函数初始化
public function __construct($dbParam = array()) {
$this-config = (is_array($dbParam)!empty($dbParam)) ? $dbParam : AppConfig::$dbParam;
$this-connect();
$this-init();
}
//链接数据库
private function connect() {
if($this-config['dbPconnect']) {
self::$link = @mysql_pconnect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword']);
}else{
self::$link = @mysql_connect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword'], true);
}
mysql_errno(self::$link) != 0$this-errdie('Could not connect Mysql: ');
$this-db= !empty($this-db) ? $this-db : $this-config['dbName'];
$serverinfo = $this-version();
if ($serverinfo'4.1'$this-config['dbCharset']) {
mysql_query("SET character_set_connection=".$this-config['dbCharset'].",character_set_results=".$this-config['dbCharset'].",character_set_client=binary", self::$link);
}
if ($serverinfo'5.0') {
mysql_query("SET sql_mode=''", self::$link);
}
@mysql_select_db($this-db, self::$link) or $this-errdie('Cannot use database');
return self::$link;
}
//表基本信息初始化
protected function init() {
$this-class = get_class($this);
$this-table = !empty($this-table) ? $this-table : strtolower($this-class);
$this-table = $this-tablepre . $this-table;
return $this;
}
//设置属性值
public function __set($name, $value) {
//exit($value);
$this-data['fields'][$name] = $value;
}
//获取属性值
public function __get($name) {
if(isset($this-data['fields'][$name])) {
return($this-data['fields'][$name]);
}else {
return NULL;
}
}
//字段信息处理
private function implodefields($data) {
if (!is_array($data)) {
$data = https://www.04ip.com/post/array();
}
$this-fields = !empty($this-data['fields']) ? array_merge($this-data['fields'], $data) : $data;
foreach($this-fields as $key = $value) {
$fieldsNameValueStr[] = "`$key`='$value'";
$fieldsNameStr[] = "`$key`";
$fieldsValueStr[] = "'$value'";
}
return array($fieldsNameValueStr, $fieldsNameStr, $fieldsValueStr);
}
//条件判断组装
private function condition($where = NULL) {
if (is_numeric($where)) {
$where = "WHERE `{$this-primaryKey}`='{$where}' LIMIT 1";
}elseif (is_array($where)){
$where = "WHERE `{$this-primaryKey}` in (".implode(',',$where).")";
}elseif(!empty($this-data['condition'])){
//'预留WHERE', 'order', 'group', 'limit' …………等条件关键词处理接口
$where = $where ? "WHERE {$where}" : "WHERE 1";
isset($this-data['condition']['where'])$where .=' AND '.$this-data['condition']['where'];
isset($this-data['condition']['group'])$where .=' GROUP BY '.$this-data['condition']['group'];
isset($this-data['condition']['order'])$where .=' ORDER BY '.$this-data['condition']['order'];
推荐阅读
- 微信视频号怎么彻底关闭,微信视频号如何彻底关闭
- ipad电脑是什么,平板电脑ipad是什么意思
- 射击游戏中心,射击游戏中心在哪
- 钉钉直播字体怎么变小点,钉钉直播字体怎么变小点了
- vbnet窗体截图 vb截图本窗体
- 不花钱怎么买电脑显卡,2021怎么买显卡
- mysql查询出身高最高的学生,mysql查询成绩最高的学生信息
- 怎样测试python模块安装成功,怎么验证python有没有安装好
- python列表倒置函数 python列表倒着取值