Query keywords for php Data Access

Source: Internet
Author: User
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} <? 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" ";}?>
Code Vehicle Name Fuel consumption Power Price
{$ 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} <? 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" ";}?>
Code Vehicle Name Series Price Fuel consumption Power
{$ 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.

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.