php模糊查询实例代码

header("content-type:text/html;charset=utf-8");
//获取用户数据
$keywords=$_POST['keywords'];
//连接数据库
$conn=@mysql_connect('localhost','root','') or die('数据库链接失败');
//选择数据库,设置字符集
mysql_select_db('search');
mysql_set_charset('utf8');
//php模糊查询
if(empty($keywords)){
    $str= "请输入要查询的内容";
}else{
$sql="SELECT * FROM user WHERE username LIKE '%$keywords%'";
}
$rs=mysql_query($sql);
$users = array();
while($row=mysql_fetch_assoc($rs)){
    //高亮替换
    $row['username'] = str_replace($keywords, ''.$keywords.'', $row['username']);
    $users[] = $row;
}
//print_r($users);
?>




php模糊查询


php模糊查询器之用户查询



用户名:

if($keywords){
    echo '

查询关键字   '.$keywords.' 结果是:

';
if($users){
    echo "";
    echo "";
    foreach ($users as $key => $value){
        echo "";
        echo "";
        echo "";
        echo "";
        echo "";
        echo "";
        echo "";
    }
    echo "
UID用户名性别邮箱兴趣爱好
".$value['uid']."".$value['username']."".$value['sex']."".$value['email']."".$value['hobby']."
";
}else {
    echo "没有查询到相关用户";
}
}else{
     echo "

".$str."


";
}
?>

你可能感兴趣的:(php实例代码,php,代码,模糊查询)