博客列表 >11月21日—PDO操作数据库

11月21日—PDO操作数据库

曾龙宇
曾龙宇原创
2019年11月22日 10:50:22498浏览

PDO连接数据库

  1. <?php
  2. //数据库连接参数
  3. $host = 'localhost';
  4. $dbname = 'demo';
  5. $username = 'root';
  6. $pwd = 'root';
  7. $dsn = "mysql:host=$host;dbname=$dbname";
  8. try{
  9. $pdo = new PDO($dsn,$username,$pwd);
  10. }catch(PDOException $e){
  11. die('连接失败:'.$e->getMessage());
  12. }

PDO增加数据

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require 'connect.php';
  4. $sql = 'INSERT INTO `student` (`name`,`phone`,`collegeId`) VALUES (:name,:phone,:collegeId)';
  5. $stmt = $pdo->prepare($sql);
  6. $name = '荆轲';
  7. $phone = '10086';
  8. $collegeId = 1;
  9. $stmt->bindParam('name',$name,PDO::PARAM_STR);
  10. $stmt->bindParam('phone',$phone,PDO::PARAM_STR);
  11. $stmt->bindParam('collegeId',$collegeId,PDO::PARAM_INT);
  12. if ($stmt->execute()){
  13. if ($stmt->rowCount()>0){
  14. echo '添加成功,返回主键ID是:'.$pdo->lastInsertId();
  15. }else{
  16. echo '添加失败';
  17. }
  18. }else{
  19. die(print_r($stmt->errorInfo(),true));
  20. }
  21. $pdo = null;

PDO编辑数据

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require 'connect.php';
  4. $sql = 'UPDATE `student` SET `name`=:name,`phone`=:phone,`collegeId`=:collegeId WHERE `studentId`=:studentId';
  5. $stmt = $pdo->prepare($sql);
  6. $studentId = 5;
  7. $name = '编辑后的名字';
  8. $phone = '12345678';
  9. $collegeId = 2;
  10. $stmt->bindParam('studentId',$studentId,PDO::PARAM_INT);
  11. $stmt->bindParam('name',$name,PDO::PARAM_STR);
  12. $stmt->bindParam('phone',$phone,PDO::PARAM_STR);
  13. $stmt->bindParam('collegeId',$collegeId,PDO::PARAM_INT);
  14. if ($stmt->execute()){
  15. if ($stmt->rowCount()>0){
  16. echo '编辑成功';
  17. }else{
  18. echo '编辑失败';
  19. }
  20. }else{
  21. die(print_r($stmt->errorInfo(),true));
  22. }
  23. $pdo = null;

PDO删除数据

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require 'connect.php';
  4. $sql = 'DELETE FROM `student` WHERE `studentId`=:studentId';
  5. $stmt = $pdo->prepare($sql);
  6. $studentId = 6;
  7. $stmt->bindParam('studentId',$studentId,PDO::PARAM_INT);
  8. if ($stmt->execute()){
  9. if ($stmt->rowCount()>0){
  10. echo '删除成功';
  11. }else{
  12. echo '删除失败';
  13. }
  14. }else{
  15. die(print_r($stmt->errorInfo(),true));
  16. }
  17. $pdo = null;

PDO查询数据—查询所有数据

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require 'connect.php';
  4. $sql = 'SELECT * FROM `student`';
  5. $stmt = $pdo->prepare($sql);
  6. $stmt->execute();
  7. $cates = $stmt->fetchAll(PDO::FETCH_ASSOC);
  8. foreach($cates as $cate){
  9. echo '学生信息:'.$cate['studentId'].'-'.$cate['name'].'-'.$cate['phone'].'-'.$cate['collegeId'];
  10. echo '<br>';
  11. }

PDO查询数据—查询单条数据

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require 'connect.php';
  4. $sql = 'SELECT * FROM `student` WHERE `studentId`=:studentId';
  5. $stmt = $pdo->prepare($sql);
  6. $studentId = 2;
  7. $stmt->bindParam('studentId',$studentId,PDO::PARAM_INT);
  8. $stmt->execute();
  9. while($cate = $stmt->fetch(PDO::FETCH_ASSOC)){
  10. echo '学生信息:'.$cate['studentId'].'-'.$cate['name'].'-'.$cate['phone'].'-'.$cate['collegeId'];
  11. }
  12. $pdo = null;

手抄作业






声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议