Blogger Information
Blog 26
fans 1
comment 0
visits 22288
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 第课:PDO增删改查操作 以及常用预处理方法 11月21日
孤忆寻昔R
Original
727 people have browsed it

作业一

添加操作: insert into 表名 SET name=:n alias=:a’

  1. <?php
  2. //添加操作
  3. require __DIR__.'/pdo.php';
  4. $sql = 'INSERT INTO `works` SET `name`=:n, `alias`=:a';
  5. $stmt = $pdo->prepare($sql);
  6. $name = 'sdfasdf';
  7. $alias = '30';
  8. $stmt->bindParam('n',$name,PDO::PARAM_STR);
  9. $stmt->bindParam('a',$alias,PDO::PARAM_STR);
  10. $add = $stmt->execute();
  11. if($add){
  12. $count = $stmt->rowCount();
  13. if($count > 0){
  14. echo '插入成功';
  15. }else{
  16. echo '插入失败';
  17. }
  18. } else{
  19. die($stmt-> errorInfo());
  20. }
  21. $pdo = null;


删除操作 ‘DELETE * FROM 表名 WHERE id=:i’

  1. <?php
  2. //删除操作
  3. require __DIR__.'/pdo.php';
  4. $sql = 'DELETE FROM `works` WHERE `id`=:i';
  5. $stmt = $pdo -> prepare($sql);
  6. $id = 12;
  7. $stmt->bindParam('i',$id,PDO::PARAM_INT);
  8. if($stmt -> execute()){
  9. if ($stmt -> rowCount()> 0) {
  10. echo '删除数据成功'.$stmt -> rowCount();
  11. }
  12. } else {
  13. die(print_r($stmt->errorInfo(),true));
  14. }


更新操作 UPDATE 表名 SET name=:n alias=:a’

  1. <?php
  2. //更新操作
  3. require __DIR__.'/pdo.php';
  4. $sql = 'UPDATE `works` SET `name`=:n,`alias`=:a WHERE`id`=:d';
  5. $stmt = $pdo -> prepare($sql);
  6. $id = 2;
  7. $name = '张三';
  8. $alias = '山东理工大学';
  9. $stmt->bindParam('d',$id,PDO::PARAM_STR);
  10. $stmt->bindParam('n',$name,PDO::PARAM_STR);
  11. $stmt->bindParam('a',$alias,PDO::PARAM_STR);
  12. $upa = $stmt -> execute();
  13. if($upa){
  14. $count = $stmt -> rowCount();
  15. if ($count > 0) {
  16. echo '修改数据成功';
  17. } else {
  18. echo '修改数据失败';
  19. }
  20. } else {
  21. die(print_r($stmt->errorInfo(),true));
  22. }


查询操作 SELECT * FROM 表名; foreach

  1. <?php
  2. //查询操作
  3. require __DIR__.'/pdo.php';
  4. $sql = 'SELECT * FROM `works`';
  5. $stmt = $pdo -> prepare($sql);
  6. $select = $stmt->execute();
  7. if($select){
  8. $arr = $stmt->fetchAll();
  9. foreach($arr as $v){
  10. echo $v['id'].'-----'.$v['name'].'-----'.$v['alias'].'<hr>';
  11. }
  12. } else{
  13. die(print_r($stmt->errorInfo(),true));
  14. }


作业二

while查询

fetchAll()查询

  1. <?php
  2. # 查询2: fetchAll()
  3. // 1. 连接数据库
  4. require __DIR__ . '/pdo.php';
  5. // 2. 创建SQL语句模板
  6. $sql = 'SELECT * FROM `works` WHERE `id` = :d';
  7. // 3. 创建SQL语句对象
  8. $stmt = $pdo->prepare($sql);
  9. // 4. 将变量绑定到SQL语句模板的命名占位符上
  10. $id = 2;
  11. $stmt->bindParam('d', $id, PDO::PARAM_INT);
  12. // 5. 执行SQL操作
  13. $stmt->execute();
  14. // 遍历, 返回二维数组
  15. $works = $stmt->fetchAll(PDO::FETCH_ASSOC);
  16. foreach ($works as $work) {
  17. echo '<pre>' . print_r($work, true);
  18. }
  19. // 6. 销毁pdo, 关闭连接
  20. $pdo = null; // unset($pdo) 等价`

bindColumn()查询

  1. <?php
  2. # 查询3: bindColumn()
  3. // 1. 连接数据库
  4. require __DIR__ . '/pdo.php';
  5. // 2. 创建SQL语句模板
  6. $sql = 'SELECT * FROM `works` WHERE `id` = :i';
  7. // 3. 创建SQL语句对象
  8. $stmt = $pdo->prepare($sql);
  9. // 4. 将变量绑定到SQL语句模板的命名占位符上
  10. $id = 2;
  11. $stmt->bindParam('i', $id, PDO::PARAM_INT);
  12. // 5. 执行SQL操作
  13. $stmt->execute();
  14. // 将结果集中的记录中的字段, 绑定到指定的变量上
  15. $stmt->bindColumn('name', $name);
  16. $stmt->bindColumn('alias', $alias);
  17. while ($stmt->fetch(PDO::FETCH_ASSOC)) {
  18. $alias = mb_substr($alias,0, 20,'utf-8');
  19. echo '片名: ' . $name . '<br>' . '简介: ' . $alias . '...<hr>';
  20. }
  21. // 6. 销毁pdo, 关闭连接
  22. $pdo = null; // unset($pdo) 等价`

总结

1、增删改查 、 预处理 以及多种数据查询
2、预处理要注意 $sql语句和bindParam的写入方式
3、try 、catch 能够精准的判断出SQL错误方式!

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错,继续加油。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post