Blogger Information
Blog 19
fans 0
comment 1
visits 7467
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库增删改查
移动用户-4050479
Original
398 people have browsed it
  1. <?php
  2. return [
  3. //数据库类型
  4. 'type' => 'mysql',
  5. //数据库主机
  6. 'host' => 'localhost',
  7. // 用户名
  8. 'User' => 'root',
  9. // 密码
  10. 'password' => 'root',
  11. // 端口号
  12. 'port' => '3306',
  13. // 字符集
  14. 'charset' => 'utf8',
  15. //数据库
  16. 'dbName' => 'MyUser',
  17. ];
  1. <?php
  2. $config = require __DIR__.'/01.php';
  3. //print_r($config);
  4. // 解析配置
  5. ['type' => $type,'charset' => $charset, 'host' =>$host , 'User' => $user, 'password' => $password , 'dbName' =>$shuJuKu , 'port' => $port] = $config;
  6. //创建数据原
  7. $tmp = '%s:host=%s;dbname=%s;port=%s;charset=%s'; //创建模板
  8. $coon = [$type , $host , $shuJuKu , $port , $charset];
  9. $dsn = vsprintf($tmp , $coon) ;
  10. //连接数据库
  11. try{
  12. $db = new PDO($dsn, $user , $password);
  13. if ($db) {
  14. //echo '链接成功';
  15. }
  16. }catch (PDOException $e) {
  17. die ('连接失败'.$e->getMessage());
  18. }
  1. <?php
  2. require 'php/config/1202.php';
  3. $pas = 101 ;
  4. // 增
  5. $sql = 'INSERT user values (null ,?,?,?)' ;
  6. $stmt = $db->prepare($sql) ;
  7. if ($stmt->execute(['1','小王',md5($pa)])){
  8. echo 'cg'.$db->lastInsertId();
  9. }else{
  10. echo 'n------';
  11. }
  12. //改
  13. $sql = 'UPDATE user SET uname = ? WHERE ID =?' ;
  14. //判断是否有条件更新
  15. if(!stristr($sql,'where')) {
  16. die('禁止无条件更新');
  17. }
  18. $stmt = $db->prepare($sql) ;
  19. if ($stmt->execute(['白小纯','*'])){
  20. //判断影响行数
  21. echo '受影响行数 = ' . $stmt->rowCount();
  22. }else{
  23. $stmt->debugDumpParams();
  24. echo '更新失败';
  25. }
  26. // 删
  27. $sql = 'DELETE FROM user WHERE ID = ?' ;
  28. //判断是否有条件更新
  29. if(!stristr($sql,'where')) {
  30. die('禁止无条件删除');
  31. }
  32. $stmt = $db->prepare($sql) ;
  33. if ($stmt->execute(['4'])){
  34. //判断影响行数
  35. echo '删除成功 受影响行数 = ' . $stmt->rowCount();
  36. }else{
  37. $stmt->debugDumpParams();
  38. echo '更新失败';
  39. }
  40. //查
  41. $sql = 'SELECT uname = ? FROM user ' ;
  42. $stmt = $db->prepare($sql) ;
  43. if ($stmt->execute(['白小纯'])){
  44. //判断影响行数
  45. echo '查询成功 查询到行数 = ' . $stmt->rowCount();
  46. }else{
  47. $stmt->debugDumpParams();
  48. echo '查询失败';
  49. }
  50. while ($re[] = $stmt->fetch(PDO::FETCH_ASSOC));
  51. print_r($re);
Correcting teacher:PHPzPHPz

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
1 comments
墨鱼丸 2022-12-05 19:47:22
//改 $sql = 'UPDATE user SET uname = ? WHERE ID =?' ; //判断是否有条件更新 if(!stristr($sql,'where')) { die('禁止无条件更新'); } $stmt = $db->prepare($sql) ; if ($stmt->execute(['白小纯','4'])){ //判断影响行数 echo '受影响行数 = ' . $stmt->rowCount(); }else{ $stmt->debugDumpParams(); echo '更新失败'; } 有个写错的地方忘了改
1 floor
Author's latest blog post