PHP Database Condition Query

Source: Internet
Author: User
Tags php database

One, single condition query, is only one condition of the query:

1. First pick up the previously encapsulated class, then use the keyword fuzzy query:

<?php//Single QueryRequire"DBDA.class.php";//Pickup Package Class$db =NewDbda; $Sname="";//Create variables so that sname can be displayed in the form later$sql ="SELECT * from T_student";if(!empty ($_post["Sname"]))//determine if data exists{$Sname=$_post["Sname"]; $sql="SELECT * from t_student where Sname like '%{$Sname}% '";//Fuzzy Query}?>

2, create the form, submit the data to the current page, extract the keyword query:

<form action="chaxun.php"Method="Post"><!--because the query data on the current page, so submit to the current page--><div> name: <input type="text"Name="Sname"Value="<?php echo $Sname?>"/><input type="Submit"Value="Enquiry"/&GT;&LT;/DIV&GT;&LT;/FORM&GT;&LT;BR/><table width= "100%" border="1"cellpadding="0"cellspacing="0"> <tr> <td> name </td> <td> sex </td> <td> class </td> </tr >

3. Iterate through the elements in the table and turn the keyword into red:

 <? php$arr  = $db->query ($sql);  foreach  ($arr as   $v) {$str  = Str_replace ($Sname, Span style= "COLOR: #800000" > " <span style= ' color:red ' >{$Sname}</span  , $v [1 ]); //  echo  " <TR>  <td>{$str}</td> <td>{$v [2  ]}</td> <td>{$v [4 ]}</td> </tr>"  ;  } ? 

Final Result:

Second, multi-criteria query:

1, first make a form, and create a table to show it:

<table width="100%" border="1" cellpadding=" 0 " cellspacing="0"><tr>    <td> code name </td>    <td> name </td>    <td> series </td>    <td> time to market </td>    <td> Price </td ></tr></table>

2, the packaging class, and create the appropriate conditions, and verify that the data is empty:

<?Phprequire"DBDA.class.php"; $db=NewDbda ();//1. If no data is submitted, display all//2. If there is data submitted, the query is displayed according to the keyword$name =""; $TJ 1="1=1";//first condition, corresponding name, to be separated by a space$TJ 2 ="1=1";//second condition, corresponding series, to be separated by a spaceif(!empty ($_post["name"]) {$name= $_post["name"]; $TJ 1="name like '%{$name}% '";}if(!empty ($_post["Brand"]) {$brand= $_post["Brand"]; $TJ 2="brand = ' {$brand} '";}//General Conditions$TJ ="{$tj 1}and{$tj 2}"; $sql="SELECT * from car where". $tj;?>

3. Use PHP code to traverse the database table and change the keyword to red (PHP is embedded in the table):

<?Php$arr= $dbquery ($sql);foreach($arr as$v) {$str= Str_replace ($name,"<span style= ' color:red ' >{$name}</span>", $v [1]); Echo"<tr><td>{$v [0]}</td> <td>{$str}</td> <td>{$v [2]}</td> <td>{$v [3]}</td> <td>{$v [7]}</td></tr>";}?>

The end result is:

PHP Database Condition Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.