php數(shù)據(jù)訪問之查詢關(guān)鍵字
更新時(shí)間:2016年05月09日 11:51:33 作者:陌上初薰
本文根據(jù)數(shù)據(jù)庫中的car表做一個(gè)汽車查詢頁面,鞏固php查詢關(guān)鍵字操作,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了php查詢操作的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
一、一個(gè)關(guān)鍵字查詢
主頁面:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>汽車查詢頁面</title> </head> <body> <h1>汽車查詢頁面</h1> <?php include("QiChe.class.php"); $db=new QiChe(); //保留輸入查詢的內(nèi)容 $cx=""; $value=""; if(!empty($_POST["name"]))//判斷查詢內(nèi)容是否為空 { $name=$_POST["name"]; $cx=" where name like '%{$name}%'";//查詢的字符串 $value=$name; } ?> <br> <form action="QiChe.php" method="post"> <div> 請(qǐng)輸入查詢內(nèi)容:<input type="text" name="name" value="<?php echo $value; ?>"/> <input type="submit" value="查詢"/> </div> </form> <br /> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代號(hào)</td> <td>汽車名稱</td> <td>油耗</td> <td>功率</td> <td>價(jià)格</td> </tr> <?php $sql="select * from Car".$cx; $attr=$db->query($sql); foreach ($attr as $v) { //使輸入查詢的關(guān)鍵字變色,處理name //$rp="<mark>{$value}</mark>"; $rp="<span style='color:red'>{$value}</span>"; $arr=str_replace($value,$rp,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$arr}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[7]}</td> </tr>"; } ?> </table> </body> </html>
封裝類:
<?php class QiChe { public $localhost="localhost";//服務(wù)器 public $uid="root";//用戶名 public $password="";//密碼 //執(zhí)行查詢語句sql方法: //參數(shù)的含義:$sql代表要執(zhí)行的sql語句;$type代表sql語句的類型,自義0為查詢,1為其他(增刪改查);$db代表要查詢的數(shù)據(jù)庫 public function Query($sql,$type="0",$db="mydb") { $dbconnect=new MySQLi($this->localhost,$this->uid,$this->password,$db); !mysqli_connect_error() or die("連接失敗 !"); $result=$dbconnect->query($sql); if($type==0) { return $result->fetch_all(); } else { return $result; } } }
運(yùn)行結(jié)果:
二、多個(gè)關(guān)鍵字查詢
主頁面:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>汽車查詢頁面</title> </head> <body> <h1>汽車查詢頁面</h1> <br> <?php include ("./DBDA.class.php"); $db=new DBDA(); $cx=""; $value=""; $value1=""; $tj1=" 1=1";//條件1的判斷name $tj2=" 1=1";//條件2的判斷brand if(!empty($_POST["name"])) { $name=$_POST["name"]; $tj1="name like '%{$_POST['name']}%'"; $value=$name; } if(!empty($_POST["brand"])) { $name1=$_POST["brand"]; $tj2="brand= '{$_POST['brand']}'"; $value1=$name1; } $cx=" where $tj1 and $tj2";//查詢字符串 ?> <form action="ChaXun1.php" method="post"> <div> 請(qǐng)輸入名稱:<input type="text" name="name" value="<?php echo $value; ?>"/> 系列:<input type="text" name="brand" value="<?php echo $value1; ?>"> <input type="submit" name="" value="查詢"> </div> </form> <br> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代號(hào)</td> <td>汽車名稱</td> <td>系列</td> <td>價(jià)格</td> <td>油耗</td> <td>功率</td> </tr> <?php $sql="select * from Car".$cx; $attr=$db->Query($sql); foreach ($attr as $v) { //處理name //$rp="<mark>{$value}</mark>"; $rp="<span style='color:red'>{$value}</span>"; $str=str_replace($value,$rp,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[7]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } ?> </table> </body> </html>
運(yùn)行結(jié)果:
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
相關(guān)文章
php后臺(tái)程序與Javascript的兩種交互方式
在網(wǎng)頁制作過程中怎樣在不刷新頁面的情況下使前臺(tái)頁面和后臺(tái)CGI頁面保持交互一直是個(gè)問題。這里介紹兩個(gè)方法。2009-10-10PHP實(shí)現(xiàn)的一致性哈希算法完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的一致性哈希算法,以完整實(shí)例形式分析了PHP哈希算法的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11php 格式化數(shù)字的時(shí)候注意數(shù)字的范圍
因?yàn)?數(shù)據(jù)庫中 id 可能會(huì)很大 如果使用%d的話,就可能出現(xiàn)因?yàn)槌隽朔秶?而到不到正確的結(jié)果。所以,個(gè)人建議 對(duì)id進(jìn)行格式化的時(shí)候 使用%.0f 比 %d 要 好的多。2010-04-04