Blogger Information
Blog 4
fans 0
comment 0
visits 4102
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
预处理的查询源代码
陈伟龙的博客
Original
752 people have browsed it

<?php
/**
* 预处理的查询处理
*/

//连接数据库,创建连接对象
require 'public/connect.php';

//准备预处理语句
$sql = "SELECT `id`,`name`,`email` FROM `user1` WHERE `id` > ? ";
$mysqli_stmt = $mysqli->prepare($sql);
$id = 4;
$mysqli_stmt->bind_param('i', $id);

if ($mysqli_stmt->execute()) { //select语句执行会返回一个结果集
   $mysqli_stmt->store_result();  //传送一个结果集到stmt对象
   if ($mysqli_stmt->num_rows > 0) {
       //将结果集中的字段与变量进行绑定,当前返回的有三个字段
       $mysqli_stmt->bind_result($id,$name,$email);
       //开始遍历结果集,将获取到的数据与绑定的变量结合
       echo '<h3 align="center">用户信息表</h3>';
       echo '<table border="1" cellspacing="0" cellpadding="3" width="40%" align="center">';
       echo '<tr bgcolor="lightblue"><th>ID</th><th>姓名</th><th>邮箱</th></tr>';
       while ($mysqli_stmt->fetch()) { //从结果集的第一条记录开始抓取数据到绑定的变量中
           echo '<tr align="center">';
           echo '<td>'.$id.'</td><td>'.$name.'</td><td>'.$email.'</td>';
           echo '</tr>';
       }
       $mysqli_stmt->free_result(); //释放预处理结果集
       $mysqli_stmt->close();  //关闭当前的预处理语句
   } else {
       echo '<p style="color:red">当前表中没有数据~~</p>';
   }
} else {
   echo '<p>查询失败:'.$mysqli_stmt->error.'</p>';
}

//关闭连接
$mysqli->close();

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