需要开启php的pdo支持,php5.1以上版本支持
实现数据库连接单例化,有三要素 静态变量、静态实例化方法、私有构造函数 dpdo.php
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class dpdo{
private $dsn;
private $dbuser;
private $dbpwd;
private $longlink;
private $pdo;
//私有构造函数 防止被直接实例化
private function __construct($dsn, $dbuser, $dbpwd, $longlink = false) {
$this->dsn = $dsn;
$this->dbuser = $dbuser;
$this->dbpwd = $dbpwd;
$this->longlink = $longlink;
$this->connect();
}
//私有 空克隆函数 防止被克隆
private function __clone(){}
//静态 实例化函数 返回一个pdo对象
static public function instance($dsn, $dbuser, $dbpwd, $longlink = false){
static $singleton = array();//静态函数 用于存储实例化对象
$singindex = md5($dsn . $dbuser . $dbpwd . $longlink);
if (empty($singleton[$singindex])) {
$singleton[$singindex] = new self($dsn, $dbuser, $dbpwd, $longlink = false);
}
return $singleton[$singindex]->pdo;
}
private function connect(){
try{
if($this->longlink){
$this->pdo = new pdo($this->dsn, $this->dbuser, $this->dbpwd, array(pdo::attr_persistent => true));
}else{
$this->pdo = new pdo($this->dsn, $this->dbuser, $this->dbpwd);
}
$this->pdo->query('set names utf-8');
} catch(pdoexception $e) {
die('error:' . $e->getmessage() . '
');
}
}
}
用于处理字段映射,使用pdo的字段映射,可以有效避免sql注入
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//字段关联数组处理, 主要用于写入和更新数据、同and 或 or 的查询条件,产生sql语句和映射字段的数组
public function fdfields($data, $link = ',', $judge = array(), $aliastable = ''){
$sql = '';
$mapdata = array();
foreach($data as $key => $value) {
$mapindex = ':' . ($link != ',' ? 'c' : '') . $aliastable . $key;
$sql .= ' ' . ($aliastable ? $aliastable . '.' : '') . '`' . $key . '` ' . ($judge[$key] ? $judge[$key] : '=') . ' ' . $mapindex . ' ' . $link;
$mapdata[$mapindex] = $value;
}
$sql = trim($sql, $link);
return array($sql, $mapdata);
}
//用于处理单个字段处理
public function fdfield($field, $value, $judge = '=', $premap = 'cn', $aliastable = '') {
$mapindex = ':' . $premap . $aliastable . $field;
$sql = ' ' . ($aliastable ? $aliastable . '.' : '') . '`' . $field . '`' . $judge . $mapindex;
$mapdata[$mapindex] = $value;
return array($sql, $mapdata);
}
//使用刚方法可以便捷产生查询条件及对应数据数组
public function fdcondition($condition, $mapdata) {
if(is_string($condition)) {
$where = $condition;
} else if (is_array($condition)) {
if($condition['str']) {
if (is_string($condition['str'])) {
$where = $condition['str'];
} else {
return false;
}
}
if(is_array($condition['data'])) {
$link = $condition['link'] ? $condition['link'] : 'and';
list($consql, $mapcondata) = $this->fdfields($condition['data'], $link, $condition['judge']);
if ($consql) {
$where .= ($where ? ' ' . $link : '') . $consql;
$mapdata = array_merge($mapdata, $mapcondata);
}
}
}
return array($where, $mapdata);
}
增删改查的具体实现db.php
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public function fetch($sql, $searchdata = array(), $datamode = pdo::fetch_assoc, $pretype = array(pdo::attr_cursor => pdo::cursor_fwdonly)) {
if ($sql) {
$sql .= ' limit 1';
$pdostatement = $this->pdo->prepare($sql, $pretype);
$pdostatement->execute($searchdata);
return $data = $pdostatement->fetch($datamode);
} else {
return false;
}
}
public function fetchall($sql, $searchdata = array(), $limit = array(0, 10), $datamode = pdo::fetch_assoc, $pretype = array(pdo::attr_cursor => pdo::cursor_fwdonly)) {
if ($sql) {
$sql .= ' limit ' . (int) $limit[0] . ',' . (intval($limit[1]) > 0 ? intval($limit[1]) : 10);
$pdostatement = $this->pdo->prepare($sql, $pretype);
$pdostatement->execute($searchdata);
return $data = $pdostatement->fetchall($datamode);
} else {
return false;
}
}
public function insert($tablename, $data, $returninsertid = false, $replace = false) {
if(!empty($tablename) && count($data) > 0){
$sql = $replace ? 'replace into ' : 'insert into ';
list($setsql, $mapdata) = $this->fdfields($data);
$sql .= $tablename . ' set ' . $setsql;
$pdostatement = $this->pdo->prepare($sql, array(pdo::attr_cursor => pdo::cursor_fwdonly));
$execret = $pdostatement->execute($mapdata);
return $execret ? ($returninsertid ? $this->pdo->lastinsertid() : $execret) : false;
} else {
return false;
}
}
public function update($tablename, $data, $condition, $mapdata = array(), $returnrowcount = true) {
if(!empty($tablename) && count($data) > 0) {
$sql = 'update ' . $tablename . ' set ';
list($setsql, $mapsetdata) = $this->fdfields($data);
$sql .= $setsql;
$mapdata = array_merge($mapdata, $mapsetdata);
list($where, $mapdata) = $this->fdcondition($condition, $mapdata);
$sql .= $where ? ' where ' . $where : '';
$pdostatement = $this->pdo->prepare($sql, array(pdo::attr_cursor => pdo::cursor_fwdonly));
$execret = $pdostatement->execute($mapdata);
return $execret ? ($returnrowcount ? $pdostatement->rowcount() : $execret) : false;
} else {
return false;
}
}
public function delete($tablename, $condition, $mapdata = array()) {
if(!empty($tablename) && $condition){
$sql = 'delete from ' . $tablename;
list($where, $mapdata) = $this->fdcondition($condition, $mapdata);
$sql .= $where ? ' where ' . $where : '';
$pdostatement = $this->pdo->prepare($sql, array(pdo::attr_cursor => pdo::cursor_fwdonly));
$execret = $pdostatement->execute($mapdata);
return $execret;
}
}
测试文件test.php
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
header(content-type: text/html; charset=utf-8);
define('app_dir', dirname(__file__));
if (function_exists('spl_autoload_register')) {
spl_autoload_register('autoclass');
} else {
function __auto_load($classname){
autoclass($classname);
}
}
function autoclass($classname){
try{
require_once app_dir.'/class/'.$classname.'.php';
} catch (exception $e) {
die('error:' . $e->getmessage() . '
');
}
}
$db = new db();
//插入
$indata['a'] = rand(1, 100);
$indata['b'] = rand(1, 1000);
$indata['c'] = rand(1,200) . '.' . rand(1,100);
$ret = $db->insert('a', $indata);
echo '插入' . ($ret ? '成功' : '失败') . '
';
//更新
$upcondata['a'] = 100;
$upconjudge['a'] = '
$upcondata['b'] = 30;
$upconjudge['b'] = '>';
list($upconstr, $mapupcondata) = $db->fdfield('b', 200, '
$condition = array(
'str' => $upconstr,
'data' => $upcondata,
'judge' => $upconjudge,
'link' => 'and'
);
$updata['a'] = rand(1, 10);
$updata['b'] = 1;
$updata['c'] = 1.00;
$changerows = $db->update('a', $updata, $condition, $mapupcondata);
echo '更新行数:' . (int) $changerows . '
';
//删除
$delval = rand(1, 10);
list($delcon, $mapdelcon) = $db->fdfield('a', $delval);
$delret = $db->delete('a', $delcon, $mapdelcon);
echo '删除a=' . $delval . ($delret ? '成功' : '失败') . '
';
//查询
$data['a'] = '10';
$judge['a'] = '>';
$data['b'] = '400';
$judge['b'] = '
list($consql, $mapcondata) = $db->fdfields($data, 'and', $judge);
$mdata = $db->fetch('select * from a where ' . $consql . ' order by `a` desc', $mapcondata);
var_dump($mdata);
以上所述就是本文的全部内容了,希望大家能够喜欢。
http://www.bkjia.com/phpjc/1000073.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1000073.htmltecharticlephp中pdo方式实现数据库的增删改查 pdo是mysql数据库操作的一个公用类了,我们不需要进行自定类就可以直接使用pdo来操作数据库了,但是在...