PHP开发查询搜索之用户查询

用户查询

QQ截图20161201143744.png

链接数据库

<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$conn = @mysql_connect("localhost", "root", "root") or die("数据库链接错误");
mysql_select_db("sql", $conn);
mysql_query("set names 'utf8'"); //使用utf-8中文编码;
?>

然后对数据库进行模糊查询。

<?php
$sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'";
$rs= mysql_query($sql);
$users = array();//保存所以得查询到的用户
?>

使用模糊查询语句,查询出所有的用户,

保存到$users中。

QQ截图20161105114257.png


我们继续编写,把获得的数据循环遍历出来

<?php
if ($keywords){
    echo '<h3>查询关键词:<font color="red">'.$keywords.'</font></h3>';
}
if ($users){
    echo '<table width="500" cellpadding="5">';
    echo '<tr><th>用户名</th><th>密码</th><th>邮箱</th><th>性别</th><th>爱好</th>';
    foreach ($users as $key=>$value){
        echo '<tr>';
        echo '<td>'.$value['username'].'</td>';
        echo '<td>'.$value['password'].'</td>';
        echo '<td>'.$value['sex'].'</td>';
        echo '<td>'.$value['email'].'</td>';
        echo '<td>'.$value['hobby'].'</td>';
        echo '</tr>';
    }
}else{
    echo '没有查询到相关用户';
}
?>

接着我们对页面进行美化

<style>
  
    .textbox {
        width: 355px;
        height: 40px;
        border-radius: 3px;
        border: 1px solid #e2b709;
        padding-left: 10px;
    }
    .su {
        width: 365px;
        height: 40px;
        background-color: #7fbdf0;
        color: white;
        border: 1px solid #666666;
    }
    table{ background-color: #7fbdf0; line-height:25px;}
    th{ background-color:#fff;}
    td{ background-color:#fff; text-align:center}
</style>

这样我们的数据就展示了出来

QQ截图20161105114826.png


本章重点:

  1. 使用sql对数据库进行模糊查询

  2. php和html语句的混编

继续学习
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$conn = @mysql_connect("localhost", "root", "root") or die("");
mysql_select_db("sql", $conn);
mysql_query("set names 'utf8'"); //使utf-8;
//PHP
$sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'";
$rs= mysql_query($sql);
$users = array();//
if(!empty($keywords)){
while ($row=mysql_fetch_assoc($rs)){
$users[] = $row;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.textbox {
width: 355px;
height: 40px;
border-radius: 3px;
border: 1px solid #e2b709;
padding-left: 10px;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
提交重置代码
图片放大关闭