Blogger Information
Blog 27
fans 1
comment 2
visits 90417
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO常用的CURD演示
          
Original
514 people have browsed it
  1. <?php
  2. // DSN: 数据源
  3. // 数据库类型:host=数据库主机;dbname=默认数据库;port=默认端口3306;charset=默认编码utf8
  4. $dsn= 'mysql:dbname=123456';
  5. $uname= '123456';
  6. $password='555666';
  7. try {
  8. $db = new PDO($dsn,$uname,$password);
  9. // var_dump($db);
  10. if ($db){
  11. echo '连接成功'.PHP_EOL;
  12. }
  13. }catch (PDOException $e){
  14. die('连接失败'.$e->getMessage());
  15. }
  16. //执行成功的sql语句
  17. //添加
  18. //INSERT INTO `user`(`id`,`name`,`age`,`sex`) VALUES (null,'老王a ','58','男');
  19. //$sql = "INSERT `user` (`name`,`age`,`sex`) VALUES ('小龙女',18,'女')";
  20. //更新
  21. //$sql ='update `user` SET `name`="欧阳娜娜" WHERE id=5';
  22. //删除
  23. //$sql = 'DELETE FROM `user` WHERE `id`=5';
  24. //查询
  25. $sql ='SELECT `name` From `user` ';
  26. // ? 创建sql语句对象 PDOStatement的实例
  27. $stmt = $db->prepare($sql);
  28. //SQL语句
  29. echo $stmt ->debugDumpParams().PHP_EOL;
  30. // ? 执行sql
  31. if ($stmt->execute()) {
  32. // 成功
  33. echo '成功'.PHP_EOL;
  34. // 查看数据 fetchAll 获取全部 fetch:获取一组
  35. while ($result = $stmt->fetch()){
  36. print_r( $result);
  37. }
  38. } else {
  39. echo '失败';
  40. print_r($stmt->errorInfo());
  41. }

运行结果

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
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!