-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpMysqlPDO.php
55 lines (49 loc) · 1.46 KB
/
phpMysqlPDO.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
<?PHP
/**
获取连接
*/
function conn() {
$dbh = new PDO("mysql:host=localhost;port=3306;dbname=shopDB", "root", "pwd1234", array(PDO::ATTR_PERSISTENT => false));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("set names utf8mb4");
return $dbh;
}
//更新操作
function updateOp($id, $pwd , $new_pwd) {
$count = 0;
try {
$dbh = conn();
$sql = "UPDATE user set pwd=? WHERE id=? and pwd=?";
$stmt = $dbh->prepare($sql);
$stmt->execute(array($new_pwd, $id, $pwd));
//$stmt->debugDumpParams();
//print_r ($stmt->errorInfo());
$count = $stmt->rowCount();
$dbh = null;
} catch (PDOException $e) {
return -1;
}
return $count;
}
//获取list startNo=起始行号,pageSize=每页行数
function getUserList($startNo,$pageSize) {
$dbh = conn();
$sql = "SELECT id,name,group FROM user where id>(select id from user limit ?,1) limit ?";
$PDO->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$stmt = $dbh->prepare($sql);
$stmt->execute(array($startNo,$pageSize));
//$stmt->debugDumpParams();
//print_r ($stmt->errorInfo());
//echo $stmt->errorCode();
$data = array();
while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
$row = array(
'id' => $rs->id,
'name' => $rs->name,
'group' => $rs->group
);
array_push($data, $row);
}
$dbh = null;
return $data;
}