php pdo数据库封装类,一个超级适用php的pdo数据库封装类

/**

* @author Protty

* @link http://www.protty.cn

* @version 1.0

* @name MySQL PDO insert/delete/update/select

* @license PHP技术QQ交流群:136660359

**/

class server

{

private $server;

// 数据库

/**

* 构造函数,建立一个全新的mysql连接

* @param unknown $config

*/

function __construct($config)

{

$this->server = new PDO("mysql:host=$config[host];dbname=$config[dbname];charset=utf8", $config['username'], $config['password']);

$this->server->query("set names utf8");

}

/**

*

* @author protty

* @param unknown $table 该参数为数据库的表名

* @param unknown $array 例子: array("username"=>'protty',"password"=>'123456');

* @return string

*/

function insert($table, $array)

{

$into = ""; // 键名分散

$val = array(); // 值加入到预执行

$insert_num = 0; // 记录加入的个数

$create_method = ''; // 预执行参数

// 循环将传进来的$array分开,键名为数据库的字段名,键值为添加的数据,与键名一一对应

foreach ($array as $key => $value) {

$into .= "{$key},";

$val[] = $value;

$insert_num ++;

}

// 有多少个键值,就循环多少个填充问号出来填充预执行参数

for ($i = 0; $i < $insert_num; $i ++) {

$create_method .= '?,';

}

// 去掉末尾的逗号,

$create_method = rtrim($create_method, ",");

$into = rtrim($into, ",");

// 参加一条预执行sql语句,该方法为pdo对象

$Model = $this->server->prepare("insert into {$table} ({$into}) values ({$create_method})");

$exe = $Model->execute($val); // 该方法为开始执行预处理sql语句

$method_id = $this->server->lastInsertId(); // 该方法为添加后该记录的id

return $method_id; // 返回该记录id

}

/**

*

* @author protty

* @param unknown $table 该参数为数据库的表名

* @param unknown $array 例子: array("username"=>'protty',"password"=>'123456');

* @param string $where 该参数为 判断条件 例子: id=12

* @return boolean

*/

function update($table, $array, $where = '')

{

$data = ""; // 设置数据

$update_data = array(); // 设置预执行的数据

$ifwhere = ''; // where判断语句

foreach ($array as $key => $value) {

$data .= "{$key}=?,"; // 通过键名自动设置数据

$update_data[] = $value; // 通过键值自动设置修改的数据

}

$data = rtrim($data, ","); // 去掉右边逗号

// 加入where判断语句

if (! empty($where)) {

$ifwhere = "where {$where}";

}

$Model = $this->server->prepare("update {$table} set {$data} {$ifwhere}"); // 参加预执行sql语句,pdo对象方法

$exe = $Model->execute($update_data); // 该方法为开始执行预处理sql语句

return $exe; // 返回该记录id

}

/**

*

* @author protty

* @param unknown $table 该参数为数据库的表名

* @param unknown $where 该参数为 判断条件 例子: id=12 ,那么id是12的这行记录将会被删除, 如果不传$where条件,那么整个表的数据全部都会删除

* @return boolean

*/

function delete($table, $where = '')

{

// 加入where判断语句

if (! empty($where)) {

$ifwhere = "where {$where}";

}

$del = $this->server->exec("DELETE FROM {$table} {$ifwhere}"); // 空数组,表示这里我不需要预处理的数据

return $del; // 返回删除是否成功

}

/**

* @author protty

* @param unknown $table 该参数为数据库的表名

* @param string $join 联表查询,例子:left join table_b on table_a.fuck_id=table_b.id

* @param string $where 该参数为 判断条件 例子: id=12 或者 like %哈哈%

* @param string $desc 该参数为倒序查询,输入一个唯一的字段名,如输入 id

* @param string $limit 该参数查询限制,从什么开始..每次查询多少记录,例子: 5,10 (从5开始查,向后查10条记录)、例子2: 10(只查10条记录出来)

* @return Ambigous <>|multitype:

*/

function select($table, $where = '', $join = '', $desc = '', $limit = '')

{

$ifwhere = '';

$order = '';

$desc_limit = '';

// 加入where判断语句

if (! empty($where)) {

$ifwhere = "where {$where}";

}

// 倒序查询

if (! empty($desc)) {

$order = "order by {$desc} desc";

}

// 从什么开始..每次查询多少记录

if (! empty($limit)) {

$desc_limit = "limit {$limit}";

}

$Model = $this->server->prepare("SELECT * FROM {$table} {$join} {$ifwhere} {$order} {$desc_limit}"); // 参加预执行sql语句,pdo对象方法

$Model->execute(); // 空数组,表示这里我不需要预处理的数据

$m_data = $Model->fetchAll(2); // 二维数组

if (count($m_data) == 1) {

return $m_data[0]; // 返回一个一维数组

} else {

return $m_data; // 返回一个二维数组

}

}

/**

* @author proty

* 开启事务

*/

function openTransaction(){

$this->server->beginTransaction();

}

/**

* @author protty

* 提交事务

*/

function commit(){

$this->server->commit();

}

/**

* @author protty

* 回滚事务

*/

function rollBack(){

$this->server->rollBack();

}

/**

*

* @param 析构函数,关闭数据库连接,释放资源

*/

function __destruct()

{

// ...

$this->server = null; // 关闭连接

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值