批改状态:合格
老师批语:感觉大家都已经习惯了我的编程风格, 你也可以尝试其它风格
| id | name | password | status | create_time | |
|---|---|---|---|---|---|
| 1 | admin | admin@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 1 | 1588987748 |
| 2 | 张三 | zhangsan@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 1 | 1588987748 |
| 3 | 李四 | lisi@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 1 | 1588987748 |
| 4 | 王五 | wangwu@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 1 | 1588987748 |
| 5 | 刘六 | liuliu@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 1 | 1588987748 |
| 6 | 赵大 | zhaoda@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 0 | 1588987748 |
| 7 | 钱二 | qianer@php.cn | 7c222fb2927d828af22f592134e8932480637c0d | 0 | 1588987748 |
<?phpnamespace pdo_edu;use Exception;use PDO;//$config = require 'config/database1.php';$config = ['type' => $type ?? 'mysql','host' => $host ?? 'localhost','dbname' => $dbname ?? 'phpedu','username' => $username ?? 'root','password' => $password ?? 'root','charset' => $charset ?? 'utf8','port' => $port ?? '3306',];//print_r($config) ;$type = $config['type'];$host = $config['host'];$dbname = $config['dbname'];$username = $config['username'];$password = $config['password'];$charset = $config['charset'];$port = $config['port'];$dsn = sprintf('%s:host=%s;dbname=%s;charset=%s;port=%s',$type,$host,$dbname,$charset,$port);try{$pdo = new PDO($dsn,$username,$password);}catch (Exception $e){die($e->getMessage());}
<?php// PDO单条查询namespace pdo_edu;use PDO;//连接数据库require 'conn.php';$sql = 'SELECT `id`,`name`,`status` FROM `user` WHERE `status` =1';//预处理,防止SQL注入$stmt = $pdo->prepare($sql);//执行SQL$stmt->execute();//使用fetch获取关联数组while ($user = $stmt->fetch(PDO::FETCH_ASSOC)) {printf('<pre>%s</pre>',print_r($user,true));}//关闭连接unset($pdo);
Array
(
[id] => 1
[name] => admin
[status] => 1
)
Array
(
[id] => 2
[name] => 张三
[status] => 1
)
Array
(
[id] => 3
[name] => 李四
[status] => 1
)
Array
(
[id] => 4
[name] => 王五
[status] => 1
)
Array
(
[id] => 5
[name] => 刘六
[status] => 1
)
<?php// PDO多条查询namespace pdo_edu;use PDO;//连接数据库require 'conn.php';// 匿名占位符: ?//$sql = 'SELECT `id`,`name`,`status` FROM `user` WHERE `status` = ?';// 命名占位符: 给一个有意义 的字符串, 必须用冒号开始$sql = 'SELECT * FROM `user` WHERE `status` = :status';//预处理,防止SQL注入$stmt = $pdo->prepare($sql);//执行SQL// 对应匿名占位符使用是索引数组// $stmt->execute([1]);// 对应命名占位符使用是关联数组$stmt->execute(['status'=>1]);//使用fetch_all获取关联数组$users = $stmt->fetchall(PDO::FETCH_ASSOC);printf('|id|姓名|状态|创建时间|<br>');printf('|--|--|--|--|<br>');foreach ($users as $user) {$date = date('Y年m月d日', $user['create_time']);printf('|%s|%s|%s|%s|<br>', $user['id'], $user['name'], $user['status'], $date);// printf('id=%s:姓名=%s---状态=%s---创建时间=%s<br>', $user['id'], $user['name'], $user['status'], $date);}//关闭连接unset($pdo);
| id | 姓名 | 状态 | 创建时间 |
|---|---|---|---|
| 1 | admin | 1 | 2020年05月09日 |
| 2 | 张三 | 1 | 2020年05月09日 |
| 3 | 李四 | 1 | 2020年05月09日 |
| 4 | 王五 | 1 | 2020年05月09日 |
| 5 | 刘六 | 1 | 2020年05月09日 |
<?phpnamespace pdo_edu;use PDO;require 'conn.php';$sql = 'INSERT `user` SET `name` = ?,`password` = ?,`email`=?,`status` =? ,`create_time` = ?';$stmt = $pdo->prepare($sql);$data = ['刘七','7c222fb2927d828af22f592134e8932480637c0d','liuqi@php.cn','1','1588987759'];$stmt->execute($data);if ($stmt->rowCount() === 1) {echo '新增成功, 新增记录的主键是: ' . $pdo->lastInsertId();} else {echo '新增失败';print_r($stmt->errorInfo());}// 关闭连接unset($pdo);
新增成功, 新增记录的主键是: 9
<?phpnamespace pdo_edu;use PDO;require 'conn.php';$sql = 'DELETE FROM `user` WHERE `id`= :id';$stmt = $pdo->prepare($sql);$stmt->execute(['id'=>$_GET['id']]);if ($stmt->rowCount() === 1){printf('删除成功!');}unset($pdo);
删除成功!
<?phpnamespace pdo_edu;use PDO;require 'conn.php';$sql = 'UPDATE `user` SET `name`= ? ,`email` = ? WHERE `id`= ?';$stmt = $pdo->prepare($sql);$data = ['王老师','wanglaoshi@php.cn','4'];$stmt->execute($data);if ($stmt->rowCount() === 1){echo "更新成功";}else{echo "更新失败";print_r($stmt->errorInfo());}unset($pdo);
更新成功
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号