<h1>查询数据</h1> <form action="chaxun.php" method="post"> <!--建立表单 action 查询后返回本页面显示结果--> <div>请输入:<input type="text" name="xingming" /> <input type="submit" value="查询" /></div> <!--name是服务端用的,定义个名称--> <br /> </form> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>学号</td> <td>姓名</td> <td>性别</td> <td>班级</td> </tr> <?php $tj = " 1=1 "; //做了一个恒成立的条件 $xingming = ""; if(!empty($_POST)){ //判断值是否为空 $xingming = $_POST["xingming"]; $tj = " xingming like '%{$xingming}%' "; //用like建立模糊查询 } $db = new MySQLi("localhost","root","5270437","ceshi"); $sql = "select * from xueshengbiao where {$tj}"; //查找表加条件 echo $sql; //输出一下看看是否连接 $relsut = $db->query($sql); if($relsut){ $arr = $relsut->fetch_all(); foreach($arr as $v){ $v[1]; $xingming; $carname = str_replace($xingming,"<span style='color:red'>{$xingming}</span>",$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$carname}</td> <td>{$v[2]}</td> <td>{$v[4]}</td> </tr>"; } } ?> </table>