本文实例讲述了php实现的pdo公共类定义与用法。分享给大家供大家参考,具体如下:
db.class.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
class db extends \\PDO {
private static $_instance = null;
protected $dbName = '' ;
protected $dsn ;
protected $dbh ;
public function __construct( $dbHost , $dbUser , $dbPasswd , $dbName , $dbCharset = 'utf8' ) {
try {
$this ->dsn = 'mysql:host=' . $dbHost . ';dbname=' . $dbName ;
$this ->dbh = new \\PDO( $this ->dsn, $dbUser , $dbPasswd );
$this ->dbh->setAttribute(\\PDO::ATTR_EMULATE_PREPARES, false);
$this ->dbh->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_EXCEPTION);
$this ->dbh-> exec ( 'SET character_set_connection=' . $dbCharset . ';SET character_set_client=' . $dbCharset . ';SET character_set_results=' . $dbCharset );
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public static function getInstance( $dbHost , $dbUser , $dbPasswd , $dbName , $dbCharset = 'utf8' ) {
if (self:: $_instance === null) {
self:: $_instance = new self( $dbHost , $dbUser , $dbPasswd , $dbName , $dbCharset );
}
return self:: $_instance ;
}
public function fetchAll( $sql , $params = array ()) {
try {
$stm = $this ->dbh->prepare( $sql );
if ( $stm && $stm ->execute( $params )) {
return $stm ->fetchAll(\\PDO::FETCH_ASSOC);
}
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public function fetchOne( $sql , $params = array ()) {
try {
$result = false;
$stm = $this ->dbh->prepare( $sql );
if ( $stm && $stm ->execute( $params )) {
$result = $stm ->fetch(\\PDO::FETCH_ASSOC);
}
return $result ;
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public function fetchColumn( $sql , $params = array ()) {
$result = '' ;
try {
$stm = $this ->dbh->prepare( $sql );
if ( $stm && $stm ->execute( $params )) {
$result = $stm ->fetchColumn();
}
return $result ;
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public function insert( $table , $params = array (), $returnLastId = true) {
$_implode_field = '' ;
$fields = array_keys ( $params );
$_implode_field = implode( ',' , $fields );
$_implode_value = '' ;
foreach ( $fields as $value ) {
$_implode_value .= ':' . $value . ',' ;
}
$_implode_value = trim( $_implode_value , ',' );
$sql = 'INSERT INTO ' . $table . '(' . $_implode_field . ') VALUES (' . $_implode_value . ')' ;
try {
$stm = $this ->dbh->prepare( $sql );
$result = $stm ->execute( $params );
if ( $returnLastId ) {
$result = $this ->dbh->lastInsertId();
}
return $result ;
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public function update( $table , $params = array (), $where = null) {
$_implode_field = '' ;
$_implode_field_arr = array ();
if ( empty ( $where ) ) {
return false;
}
$fields = array_keys ( $params );
foreach ( $fields as $key ) {
$_implode_field_arr [] = $key . '=' . ':' . $key ;
}
$_implode_field = implode( ',' , $_implode_field_arr );
$sql = 'UPDATE ' . $table . ' SET ' . $_implode_field . ' WHERE ' . $where ;
try {
$stm = $this ->dbh->prepare( $sql );
$result = $stm ->execute( $params );
return $result ;
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public function delete ( $sql , $params = array ()) {
try {
$stm = $this ->dbh->prepare( $sql );
$result = $stm ->execute( $params );
return $result ;
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
public function exec ( $sql , $params = array ()) {
try {
$stm = $this ->dbh->prepare( $sql );
$result = $stm ->execute( $params );
return $result ;
} catch (Exception $e ) {
$this ->outputError( $e ->getMessage());
}
}
private function outputError( $strErrMsg ) {
throw new Exception( "MySQL Error: " . $strErrMsg );
}
public function __destruct() {
$this ->dbh = null;
}
}
|
实例:
?
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
|
<?php
require_once './db.class.php' ;
$pdo = db::getInstance( '127.0.0.1' , 'root' , '111111' , 'php_cms' );
$sql = "select id, title1 from cms_wz where id = :id limit 1" ;
$parame = array ( 'id' => 12,);
$res = $pdo ->fetchOne( $sql , $parame );
var_dump( $res );
$sql = 'SELECT * FROM cms_link' ;
$result = $db ->fetchAll( $sql );
print_r( $result );
//查询记录数量
$sql = 'SELECT COUNT(*) FROM cms_link' ;
$count = $db ->fetchColumn( $sql );
echo $count ;
$data = array (
'siteid' => 1,
'linktype' => 1,
'name' => 'google' ,
'url' => 'http://www.google.com' ,
'listorder' => 0,
'elite' => 0,
'passed' => 1,
'addtime' => time()
);
$lastInsertId = $db ->insert( 'cms_link' , $data );
echo $lastInsertId ;
//用 try
try {
$result = $pdo ->insert( 'news' , $essay );
} catch (Exception $e ) {
error_log ( $e ->getMessage());
error_log ( $e ->getMessage() . ' in ' . __FILE__ . ' on line ' . __LINE__ );
saveLog( 'url文章 : ' . $essay [ 'link' ] . ' 数据插入失败<br>' );
continue ;
}
$data = array (
'siteid' => 1,
'linktype' => 1,
'name' => 'google' ,
'url' => 'http://www.google.com' ,
'listorder' => 0,
'elite' => 0,
'passed' => 1,
'addtime' => time()
);
$db ->insert( 'cms_link' , $data );
$sql = 'DELETE FROM cms_link WHERE linkid=4' ;
$result = $db -> delete ( $sql );
var_dump( $result );
|
希望本文所述对大家PHP程序设计有所帮助。
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 60
-
2025-05-27 34
-
2025-05-29 84
-
2025-05-29 67
-
2025-06-04 13
热门评论