21,893
社区成员




<?php
$mysqli = new mysqli("localhost","root","","ims");
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
if(isset($_POST['searchuserid'])){
$id = $_POST["rolename"];
$sql = "select * from `t_userinfo` where u_id='$id'";
$result = $mysqli->query($sql);
}
?>
<?php
while($row = $result->fetch_array()){?>
<tr>
<td><?php echo $row["u_id"]?></td>
<td><?php echo $row["u_name"]?></td>
<td><?php if ($row["u_state"] == 1){
echo "正在使用";
}else if ($row["u_state"] == 0){
echo "该编号已过期";};?></td>
<td>
<a href="edit.html">编辑</a>
<a href="">删除</a>
<a href="">详细</a>
</td>
</tr><?php } ?>
if(is_a($result, 'mysqli_result')) {
....
}
仅判断是否为对象(is_object)也可以,但是不严密
一般的说,你应该总是执行查询,这样就没有问题了
$sql = "select * from `t_userinfo` where 1=1"; //查询全部数据
//$sql = "select * from `t_userinfo` where 1=2"; //什么也查不到
if(isset($_POST['searchuserid'])){
$id = $_POST["rolename"];
$sql = "select * from `t_userinfo` where u_id='$id'";
}
$result = $mysqli->query($sql);
while($row = $result->fetch_array()) {
....
}
<?php
$mysqli = new mysqli("localhost","root","","ims");
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
if(isset($_POST['searchuserid'])){
$id = $_POST["rolename"];
$sql = "select * from `t_userinfo` where u_id='$id'";
$result = $mysqli->query($sql);
$row = $result->fetch_array()
}
?>
这样下面的html里面的while($row)就会反复的执行,这个我明白是什么原因!但是跟开始说的一样,如果把$row = $result->fetch_array()放在while里又会出错!