Blogger Information
Blog 34
fans 0
comment 0
visits 22395
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月21日—PDO操作数据库
曾龙宇
Original
492 people have browsed it

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;

手抄作业






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