This article makes a car query page based on the car table in the database to consolidate the php keyword query operations. interested partners can refer to the example in this article to share the php query operation implementation code, for your reference, the specific content is as follows:
1. a keyword query
Home page:
Car query page Car query page <? Php include ("QiChe. class. php "); $ db = new QiChe (); // retain the content of the input query $ cx =" "; $ value =" "; if (! Empty ($ _ POST ["name"]) // determines whether the query content is null {$ name = $ _ POST ["name"]; $ cx = "where name like '% {$ name} %'"; // query string $ value = $ name ;}?>
{$ Value}
| Code |
Vehicle Name |
Fuel consumption |
Power |
Price |
<? Php $ SQL = "select * from Car ". $ cx; $ attr = $ db-> query ($ SQL); foreach ($ attr as $ v) {// color the keyword of the input query, process name // $ rp =""; $ Rp =" {$ value} "; $ arr = str_replace ($ value, $ rp, $ v [1]); echo"
| {$ V [0]} |
{$ Arr} |
{$ V [4]} |
{$ V [5]} |
{$ V [7]} |
";}?>
Encapsulation class:
<? Phpclass QiChe {public $ localhost = "localhost"; // server public $ uid = "root"; // User name public $ password = ""; // password // SQL method for executing the query statement: // parameter description: $ SQL indicates the SQL statement to be executed; $ type indicates the type of the SQL statement, and 0 indicates the query, 1 is other (add, delete, modify, and Query); $ db indicates the public function Query of the database to be queried ($ SQL, $ type = "0", $ db = "mydb ") {$ dbconnect = new MySQLi ($ this-> localhost, $ this-> uid, $ this-> password, $ db );! Mysqli_connect_error () or die ("connection failed! "); $ Result = $ dbconnect-> query ($ SQL); if ($ type = 0) {return $ result-> fetch_all ();} else {return $ result ;}}}
Running result:
2. query multiple keywords
Home page:
Car query page Car query page
<? Phpinclude (". /DBDA. class. php "); $ db = new DBDA (); $ cx =" "; $ value =" "; $ value1 =" "; $ tj1 =" 1 = 1 "; // name $ tj2 = "1 = 1" for condition 1; // brandif for condition 2 (! 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 "; // query string?>
{$ Value}
| Code |
Vehicle Name |
Series |
Price |
Fuel consumption |
Power |
<? Php $ SQL = "select * from Car ". $ cx; $ attr = $ db-> Query ($ SQL); foreach ($ attr as $ v) {// process name // $ rp =""; $ Rp =" {$ value} "; $ str = str_replace ($ value, $ rp, $ v [1]); echo"
| {$ V [0]} |
{$ Str} |
{$ V [2]} |
{$ V [7]} |
{$ V [4]} |
{$ V [5]} |
";}?>
Running result:
The above is all the content of this article. I hope it will help you learn php programming.