php mysqli 预处理操作数据库

布满荆棘的人生 2021-11-23 01:52 473阅读 0赞

用到的SQL表

  1. CREATE TABLE `student_01` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '名字',
  4. `kecheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  5. `score` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  6. `other_id` int(11) DEFAULT NULL,
  7. PRIMARY KEY (`id`) USING BTREE,
  8. KEY `aaaa` (`other_id`) USING BTREE
  9. ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
  10. 输入变量的数据处理
  11. //输入变量的数据处理
  12. //输入变量的过程如下:
  13. // 01) 预备(解析)语句 02) 绑定变量 03) 赋值到绑定变量 04) 执行预备语句
  14. $conn = mysqli_connect('127.0.0.1', 'afei2', '123456', 'test');
  15. $conn->query("set names utf8mb4");
  16. $stmt = $conn->prepare("INSERT INTO student_01(name, kecheng, score,other_id) VALUES (?, ?, ?, ?)");
  17. $stmt->bind_param('ssdi',$name, $kecheng, $score,$other_id);//第一个参数是指定类型
  18. $name = '大飞';
  19. $kecheng = '数学';
  20. $score = 75;
  21. $other_id = 1;
  22. $stmt->execute();
  23. $name = '大飞02';
  24. $kecheng = '语文';
  25. $score = 60;
  26. $other_id = 1;
  27. $stmt->execute();
  28. $stmt->close();
  29. 绑定变量获取的例子
  30. //绑定变量获取的例子
  31. //输出变量的过程如下:
  32. // 01) 预备(解析)语句 02) 执行预备语句 03) 绑定输出变量 04) 把数据提取到输出变量中
  33. $conn = mysqli_connect('127.0.0.1', 'afei2', '123456', 'test');
  34. $conn->query("set names utf8mb4");
  35. $stmt = $conn->prepare("SELECT id,name,kecheng,score FROM student_01");
  36. $stmt->bind_result($id, $name,$kecheng,$score);//这里定义的变量
  37. $stmt->execute();
  38. print "<table border='1' >" . PHP_EOL;
  39. print "<tr><th>ID</th><th>姓名</th><th>课程</th><th>分数</th></tr>" . PHP_EOL;
  40. while ($stmt->fetch()) {
  41. print "<tr><td>$id</td><td>$name</td><td>$kecheng</td><td>$score</td></tr>" . PHP_EOL;
  42. }
  43. print "</table>" . PHP_EOL;
  44. $stmt->close();

  

  

转载于:https://www.cnblogs.com/dafei4/p/11227697.html

发表评论

表情:
评论列表 (有 0 条评论,473人围观)

还没有评论,来说两句吧...

相关阅读