课程代码: <?php $keywrods=isset($_GET['keywrods'])?trim($_GET['keywrods']):''; $conn=@mysql_connect('127.0.0.3','root','root')or die('连接失败'); mysql_query('use user');//选择数据库 mysql_query('set names utf8');//设置字符编码 //关键词 $sql="select * from user where username like '%{$keywrods}%'"; $rs=mysql_query($sql);//查询结果集 $users=array();//用这个数组保存查询到的数据 if(!empty($keywrods)){//判断传入的数据不为空时,才执行while while ($row=mysql_fetch_assoc($rs)) { //对用户名进行关键词高亮 $row['username']=str_replace($keywrods, '<font color="red">'.$keywrods.'</font>',$row['username']); $users[]=$row; } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>搜索</title> </head> <body> <h1>PHP模糊查询</h1> <form action="" method="get"> 用户名: <input type="text" name="keywrods" value="" /> <input type="submit" value="查询" /> </form> <?php if($keywrods){ echo '<h3>查询关键词<font color="red">'.$keywrods.'</font>的结果</h3>'; } if($users){ echo '<table width="500" border="1" cellpadding="5">'; echo '<tr bgcolor="orange"><th>用户名:</th><th>邮箱地址:</th><th>性别:</th><th>爱好:</th></tr>'; foreach ($users as $k => $v) { echo '<tr>'; echo '<td>'.$v['username'].'</td>'; echo '<td>'.$v['email'].'</td>'; echo '<td>'.$v['sex'].'</td>'; echo '<td>'.$v['hobby'].'</td>'; echo '</tr>'; } echo '</table>'; }else{ echo "没有查询到相关用户"; } ?> </body> </html>
东写西读
2014-11-04
0 回答
举报
0/150
提交
取消