單條件查詢:
1.先要有一張表,顯示出表中的數(shù)據(jù):
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
34
35
36
37
38
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> </head> <body> <table border= "1" cellspacing= "0" cellpadding= "0" > <tr> <td width= "200" >編號</td> <td width= "200" >姓名</td> <td width= "200" >電話</td> <td width= "200" >分組</td> </tr> <?php $db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" ); $sql = "select * from contacts" ; $r = $db ->query( $sql ); //傳值 while ( $attr = $r ->fetch_row()) { echo " <tr> <td>{ $attr [0]}</td> <td>{ $attr [1]}</td> <td>{ $attr [2]}</td> <td>{ $attr [3]}</td> </tr>"; } ?> </table> </body> </html> |
上圖:
啥都沒改的一張表
2.再來個(gè)from表單,讓用戶輸入,點(diǎn)擊查詢:
1
2
3
4
5
6
7
8
|
<form action= "shouye.php" method= "post" > <div> 輸入名字:<input type= "text" name= "name" /> <input type= "submit" value= "查詢" /> </div> </form> |
如圖:
3.建立關(guān)鍵字查詢:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<?php //實(shí)現(xiàn)兩個(gè)邏輯 //1.如果沒有post數(shù)據(jù).查所有的 //2.如果有post數(shù)據(jù).根據(jù)條件查 $db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" ); //連接數(shù)據(jù)庫 $tj = " 1 = 1 " ; $name = "" ; //恒成立,如果沒有寫數(shù)據(jù),那就讓條件等于1=1,這個(gè)條件是查找所有的數(shù)據(jù) //如果你寫入數(shù)據(jù),按照數(shù)據(jù)查 if (! empty ( $_post )) { $name = $_post [ 'name' ]; $tj = " name like '%{$name}%'" ; } //將條件拼接到sql語句 $sql = "select * from contacts where {$tj}" ; echo $sql ; //查出來 $r = $db ->query( $sql ); //傳值 if ( $r ) //開始判斷 { //$attr已經(jīng)接收到了值,現(xiàn)在只需要獲取他的索引就行了 while ( $attr = $r ->fetch_row()) { //關(guān)鍵字特殊查詢 $str = str_replace ( $name , "<mark>{$name}</mark>" , $attr [1]); //查找替換如ctrl+f //substr_replace(); 在指定位置替換 //substr(); 截取字符串 $gname = "select gname from groups where gid='{$attr[3]}'" ; //分組表中的gid,和我點(diǎn)擊的 $nresult = $db ->query( $gname ); $gname = $nresult ->fetch_row(); $nation = $gname [0]; echo " <tr> <td>{ $attr [0]}</td> <td>{ $str }</td> <td>{ $attr [2]}</td> <td>{ $nation }</td> ?> |
圖:
多條件查詢:
前面照舊;
出了php的語句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php //實(shí)現(xiàn)兩個(gè)邏輯 //1.如果沒有post數(shù)據(jù).查所有的 //2.如果有post數(shù)據(jù).根據(jù)條件查 $db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" ); //連接數(shù)據(jù)庫 $tj1 = " 1 = 1 " ; $tj2 = " 1 = 1 " ; //兩個(gè)條件的恒等 $name = "" ; //恒成立,如果沒有寫數(shù)據(jù),那就讓條件等于1=1,這個(gè)條件是查找所有的數(shù)據(jù) //如果你寫入數(shù)據(jù),按照數(shù)據(jù)查 if (! empty ( $_post [ "name" ])) //第一個(gè)條件的判斷(用到了模糊查詢) { $name = $_post [ 'name' ]; $tj1 = " name like '%{$name}%'" ; } if (! empty ( $_post [ "tel" ])) { $tel = $_post [ "tel" ]; $tj2 = "tel = '$tel'" ; } //將條件拼接到sql語句 $sql = "select * from contacts where {$tj1} and {$tj2}" ; |
效果圖:
這樣:有幾個(gè)條件就做幾個(gè)條件變量,第一個(gè)條件不為空就執(zhí)行的第一個(gè)條件,第二個(gè)條件不為空執(zhí)行的第二個(gè)條件,兩個(gè)都為空就是查尋所有的數(shù)據(jù)