php資料訪問之查詢關鍵字_php技巧

來源:互聯網
上載者:User

本文執行個體為大家分享了php查詢操作的實現代碼,供大家參考,具體內容如下

一、一個關鍵字查詢

首頁面:

<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();  //保留輸入查詢的內容  $cx="";  $value="";  if(!empty($_POST["name"]))//判斷查詢內容是否為空白  {  $name=$_POST["name"];  $cx=" where name like '%{$name}%'";//查詢的字串  $value=$name;  }?> <br><form action="QiChe.php" method="post"><div>請輸入查詢內容:<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>代號</td><td>汽車名稱</td><td>油耗</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>";  $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>

封裝類: 

<?phpclass QiChe{  public $localhost="localhost";//伺服器  public $uid="root";//使用者名稱  public $password="";//密碼  //執行查詢語句sql方法:  //參數的含義:$sql代表要執行的sql語句;$type代表sql語句的類型,自義0為查詢,1為其他(增刪改查);$db代表要查詢的資料庫  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;    }  } }

運行結果:

二、多個關鍵字查詢

首頁面:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>汽車查詢頁面</title></head> <body><h1>汽車查詢頁面</h1><br><?phpinclude ("./DBDA.class.php");$db=new DBDA();$cx="";$value="";$value1="";$tj1=" 1=1";//條件1的判斷name$tj2=" 1=1";//條件2的判斷brandif(!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>請輸入名稱:<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>代號</td><td>汽車名稱</td><td>系列</td><td>價格</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>

運行結果:

以上就是本文的全部內容,希望對大家學習php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.