Blogger Information
Blog 36
fans 1
comment 0
visits 28826
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月21日PDO连接与操作(增删查改)-九期线上班
WJF
Original
670 people have browsed it

pdo操作:连接、增删查改操作(手写)


连接:
  1. <?php
  2. //连接数据库 1
  3. // $pdo = new PDO('mysql:host=127.0.0.1;dbname=phpcn','root','root');
  4. // print_r( $pdo );
  5. //连接数据库 2
  6. $db = [
  7. 'type' => 'mysql',
  8. 'host' => '127.0.0.1',
  9. 'dbname' => 'phpcn',
  10. 'username' => 'root',
  11. 'password' => 'root',
  12. ];
  13. $dsn = "$db[type]:host=$db[host];dbname=$db[dbname]";
  14. // $pdo = new PDO($dsn,$db['username'],$db['password']);
  15. // print_r( $pdo);
  16. //报错信息
  17. try{
  18. $pdo = new PDO($dsn,$db['username'],$db['password']);
  19. echo '连接成功!';
  20. }catch (PDOException $s){
  21. die('连接失败,错误信息:' . $s->getMessage());
  22. }


增加:
  1. <?php
  2. //连接数据库
  3. require __DIR__ .'/config.php';
  4. //创建数据库语句模板
  5. $sql = 'INSERT INTO `user` SET `name`=:name, `pass`=:pass, `time`=:time, `dltime`=:dltime, `ip`=:ip ';
  6. //创建数据库语句对象
  7. $sqlt = $pdo->prepare($sql);
  8. //绑定变量值
  9. $name = 'username';
  10. $pass = '123456';
  11. $time = '1574341957818';
  12. $dltime = '1574341957818';
  13. $ip = '127.0.0.1';
  14. $sqlt->bindParam('name',$name,PDO::PARAM_STR);
  15. $sqlt->bindParam('pass',$pass,PDO::PARAM_STR);
  16. $sqlt->bindParam('time',$time,PDO::PARAM_INT);
  17. $sqlt->bindParam('dltime',$dltime,PDO::PARAM_INT);
  18. $sqlt->bindParam('ip',$ip,PDO::PARAM_STR);
  19. // $sqlt->execute();
  20. if ($sqlt->execute()){
  21. echo '成功增加' . $sqlt->rowCount() . '条记录!' ;
  22. }else{
  23. echo '失败!';
  24. }

更新:
  1. <?php
  2. //更新记录
  3. //连接数据库
  4. require __DIR__ . '/config.php';
  5. //创建数据库语句模板
  6. $sql = 'UPDATE `user` SET `name`=:name, `pass`=:pass, `time`=:time, `dltime`=:dltime, `ip`=:ip WHERE `id`=:id';
  7. //创建数据库语句对象
  8. $sqlt = $pdo->prepare($sql);
  9. //绑定变量
  10. $name = 'username';
  11. $pass = '123456';
  12. $time = '1574341957818';
  13. $dltime = '1574341957818';
  14. $ip = '127.0.0.1';
  15. $id = 2;
  16. $sqlt->bindParam('name',$name,PDO::PARAM_STR);
  17. $sqlt->bindParam('pass',$pass,PDO::PARAM_STR);
  18. $sqlt->bindParam('time',$time,PDO::PARAM_INT);
  19. $sqlt->bindParam('dltime',$dltime,PDO::PARAM_INT);
  20. $sqlt->bindParam('ip',$ip,PDO::PARAM_STR);
  21. $sqlt->bindParam('id',$id,PDO::PARAM_INT);
  22. //执行操作
  23. $sqlt->execute();
  24. //销毁
  25. $pdo = null;

删除:
  1. <?php
  2. //删除记录
  3. require __DIR__ . '/config.php';
  4. //创建数据库语句模板
  5. $sql = 'DELETE FROM `user` WHERE `id`=:id';
  6. //创建数据库语句对象
  7. $sqlt = $pdo->prepare($sql);
  8. //绑定变量
  9. $id = 28;
  10. $sqlt->bindParam('id',$id,PDO::PARAM_INT);
  11. //执行数据库操作
  12. $sqlt->execute();
  13. //销毁
  14. $pdo = null;

查询:
  1. <?php
  2. //查询记录
  3. require __DIR__ . '/config.php';
  4. //创建数据库语句模板
  5. $sql = 'SELECT * FROM `user` WHERE `id`=:id';
  6. //创建数据库语句对象
  7. $sqlt = $pdo->prepare($sql);
  8. //绑定变量
  9. $id = 4;
  10. $sqlt->bindParam('id',$id,PDO::PARAM_INT);
  11. //执行操作
  12. $sqlt->execute();
  13. //返回
  14. // print_r($sqlt->fetchAll(PDO::FETCH_ASSOC));
  15. $s = $sqlt->fetchAll(PDO::FETCH_ASSOC);
  16. foreach ($s as $v){
  17. // print_r($v);
  18. echo "用户名:$v[name]密码:$v[pass]登陆时间:";
  19. echo date('Y-m-d H:i:s', "$v[time]");
  20. }


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