博客列表 >mysqli 和pdo 连接数据库

mysqli 和pdo 连接数据库

leverWang
leverWang原创
2020年07月23日 17:26:03641浏览

1.mysqli操作数据库

  • 1.1 INSERT 插入数据
  1. <?php
  2. //引入数据库配置文件并实例化mysqli对象
  3. require 'conn.php';
  4. //创建数据库查询语句
  5. $sql = "INSERT INTO `users`(`name`,`pwd`,`age`,`tel`) VALUES (?,?,?,?)";
  6. //mysql专有插入语句,效率高
  7. //$sql = 'INSERT `users` SET `name`=?,`pwd`=?,`age`=?,`tel`=?;';
  8. //创建预处理对象
  9. $stmt = $mysqli->prepare($sql);
  10. //$stmt->bind_param('ssis',$name,$pwd,$age,$tel);
  11. $stmt->bind_param('ssis', $name, $pwd, $age, $tel);
  12. //单条数据插入
  13. //$name='Jeanne';
  14. //$pwd=md5('123');
  15. //$age=52;
  16. //$tel='10086';
  17. //执行查询
  18. //$stmt->execute() or die('新增数据失败'.$stmt->error);
  19. //printf('新增数据成功%s条,数据id为%d',$stmt->affected_rows,$stmt->insert_id);
  20. //将数组中的值插入
  21. //$data = [
  22. // 'name' => 'fate',
  23. // 'pwd'=>sha1('123'),
  24. // 'age'=>33,
  25. // 'tel'=>'1890011228'
  26. //];
  27. //结构数组为变量
  28. //extract($data);
  29. //执行查询
  30. //$stmt->execute() or die('新增数据失败'.$stmt->error);
  31. //printf('新增数据成功%s条,数据id为%d',$stmt->affected_rows,$stmt->insert_id);
  32. //插入多条数据
  33. $users = [
  34. ['name'=> 'Quella','age'=>33, 'pwd'=>sha1('1256'),'tel'=>'188938801'],
  35. ['name'=> 'Rafael','age'=>12, 'pwd'=>sha1('12456'),'tel'=>'178922801'],
  36. ['name'=> 'Mica','age'=>55, 'pwd'=>sha1('12346'),'tel'=>'168448801'],
  37. ['name'=> '骑着蜗牛狂奔','age'=>65, 'pwd'=>sha1('13456'),'tel'=>'138966801'],
  38. ['name'=> '李白','age'=>111, 'pwd'=>sha1('23456'),'tel'=>'118988801'],
  39. ];
  40. //循环插入
  41. foreach ($users as $user){
  42. extract($user);
  43. $stmt->execute() or die('插入数据出错:'.$stmt->errno.'<br>'.$stmt->error);
  44. printf('新增数据成功%s条,数据id为%d',$stmt->affected_rows,$stmt->insert_id);
  45. }
  46. $mysqli->close();
  • 1.2 select 查询数据
  1. <?php
  2. require 'conn.php';
  3. $sql = "select `name`,`age`,`tel` from users where `id`>?";
  4. //$sql = 'SELECT * FROM `users` WHERE `id` > ?';
  5. //获取预处理对象
  6. $stmt = $mysqli->prepare($sql);
  7. $stmt->bind_param('i', $id);
  8. $id = 20;
  9. $stmt->execute() or die($stmt->error);
  10. //获取查询返回的结果集
  11. $res = $stmt->get_result();
  12. //print_r($res->num_rows);
  13. if ($res->num_rows === 0) {
  14. exit('没有查询到数据');
  15. }
  16. //使用while查询数据
  17. //while ($user = $res->fetch_assoc()) {
  18. // vprintf('姓名:%s,年龄: %d,手机: %s <br>', $user);
  19. //}
  20. //使用foreach查询
  21. foreach ($res->fetch_all() as $user){
  22. vprintf('姓名:%s,年龄: %d,手机: %s <br>', $user);
  23. }
  24. //释放结果集
  25. $res->free();
  26. //关闭连接
  27. $mysqli->close();
  • 1.3 update 更新数据
  1. <?php
  2. require 'conn.php';
  3. $sql="update `users` set `name`=?,`age`=?,`pwd`=?,`tel`=? where `id`=?";
  4. $stmt=$mysqli->prepare($sql);
  5. $stmt->bind_param('sissi',$name,$age,$pwd,$tel,$id);
  6. //$id=21;
  7. //$name='Jeanne-s';
  8. //使用数组更新
  9. $data=[
  10. 'name'=>'samss',
  11. 'age'=>22,
  12. 'pwd'=>sha1(123),
  13. 'tel'=>'111112222',
  14. 'id'=>22
  15. ];
  16. extract($data);
  17. $stmt->execute() or die('更新数据失败:'.$stmt->error);
  18. printf('更新数据成功%s条',$stmt->affected_rows);
  19. $mysqli->close();
  • 1.4 delete 删除数据
  1. <?php
  2. require 'conn.php';
  3. $sql="delete from `users` where `id`=?";
  4. //$sql="delete from `users` where `age`=?";
  5. $stmt=$mysqli->prepare($sql);
  6. $stmt->bind_param('i',$age);
  7. $id=22;
  8. //$age=0;
  9. $stmt->execute() or die('删除失败'.$stmt->error);
  10. printf ("成功删除%s条数据",$stmt->affected_rows);
  11. $mysqli->close();

2.PDO 操作数据库

  • 2.1 INSERT 插入数据
  1. <?php
  2. require 'conn.php';
  3. $sql = "INSERT INTO `users`(`name`,`pwd`,`age`,`tel`) VALUES (?,?,?,?)";
  4. $stmt=$pdo->prepare($sql);
  5. $stmt->bindParam(1,$name,PDO::PARAM_STR,50);
  6. $stmt->bindParam(2,$pwd,PDO::PARAM_STR,50);
  7. $stmt->bindParam(3,$age,PDO::PARAM_INT,30);
  8. $stmt->bindParam(4,$tel,PDO::PARAM_STR,50);
  9. $users = [
  10. ['name'=> 'Adalia','age'=>33, 'pwd'=>sha1('1256'),'tel'=>'188938801'],
  11. ['name'=> 'Brenda','age'=>12, 'pwd'=>sha1('12456'),'tel'=>'178922801'],
  12. ['name'=> 'Alice','age'=>55, 'pwd'=>sha1('12346'),'tel'=>'168448801'],
  13. ['name'=> '骑牛狂奔','age'=>65, 'pwd'=>sha1('13456'),'tel'=>'138966801'],
  14. ['name'=> 'Catherine','age'=>111, 'pwd'=>sha1('23456'),'tel'=>'118988801'],
  15. ];
  16. foreach ($users as $user){
  17. extract($user);
  18. $stmt->execute() or die(print_r($stmt->errorInfo(),true));
  19. if($stmt->rowCount()>0){
  20. printf("成功添加 %s 条数据",$stmt->rowCount());
  21. }
  22. }
  23. //关闭数据连接
  24. unset($pdo);
  • 2.2 select 查询数据
  1. <?php
  2. require 'conn.php';
  3. $sql = "select `id`,`name`,`age` from `users` where id>= ?";
  4. $stmt = $pdo->prepare($sql);
  5. $id = 22;
  6. $stmt->bindParam(1, $id, PDO::PARAM_INT, 30);
  7. $stmt->execute() or die(print_r('查询失败' . $stmt->errorInfo()));
  8. foreach ($stmt->fetchAll() as $user) {
  9. vprintf("id:%s,name:%s,age:%s<br>", $user);
  10. }
  • 2.3 update 更新数据
  1. <?php
  2. require 'conn.php';
  3. $sql="update `users` set `name`=? where id=? ";
  4. $stmt=$pdo->prepare($sql);
  5. //$stmt->bindParam(1,$name,PDO::PARAM_STR,50);
  6. //$stmt->bindParam(2,$id,PDO::PARAM_INT,20);
  7. //使用数组直接绑定
  8. $stmt->execute(['阿大',55]);
  9. if($stmt->rowCount()>0):
  10. print_r('成功更新'.$stmt->rowCount().'条数据');
  11. endif;
  12. //关闭数据连接
  13. unset($pdo);
  • 2.4 delete 删除数据
  1. <?php
  2. require 'conn.php';
  3. $sql = "delete from `users` where `id`=?";
  4. //预处理对象
  5. $stmt = $pdo->prepare($sql);
  6. //匿名占位符绑定
  7. $stmt->bindParam(1, $id, PDO::PARAM_INT, 30);
  8. $id = 42;
  9. $stmt->execute() or die(print_r('删除失败' . $stmt->errorInfo(),true) );
  10. printf("成功删除%s条数据", $stmt->rowCount());
  11. //关闭数据连接
  12. unset($pdo);

总结:熟悉了php操作数据库的基本方法和数据库查询语句的基本写法

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