Php + mysql + ajax: The method for querying multiple fields and keywords in a single table.

Source: Internet
Author: User

Php + mysql + ajax: The method for querying multiple fields and keywords in a single table.

This example describes how to query multiple fields and keywords in a single table using php + mysql + ajax. We will share this with you for your reference. The details are as follows:

Multi-field query in a single table is very useful in some queries that are slightly more complex. Here, we mainly use the concat function in the MySQL database to implement multi-field and multi-Keyword queries in a single table. The table that displays the query results can be dynamically generated based on the selected data table.

Html code:

<! DOCTYPE html> 

QueryInIndex. php:

<? Phpinclude_once (".. /phpFiles/connMysql. php "); // code containing the database connection $ queryType = $ _ POST ['querytype']; // target data table $ queryKeywords = $ _ POST ['querykeyword']; // query the word $ queryKeywords = preg_replace ('# \ s + #', '', $ queryKeywords ); // replace multiple consecutive spaces with one space in the string $ keywords = explode ("", $ queryKeywords ); // query words are separated by spaces // different query statements are generated based on different data tables if ($ queryType = "users") {$ sqlsmt = "select email, truename, enName, stuid, degree, supervisorname, tel, room from $ queryTyp E where "; // the initial value of the query statement // generate the query statement foreach ($ keywords as $ keyword) for each keyword in a loop {$ sqlsmt. = "concat (email, truename, enName, stuid, degree, supervisorname, tel, room) like '% $ keyword %' or"; // This sentence is the key, the concat function is used to query a single keyword from multiple fields and merge the concat results of each keyword with the 'or' keyword.} $ sqlsmt = substr ($ sqlsmt, 0, strlen ($ sqlsmt) -3); // remove the 'or' $ sqlsmt at the end of the query string. = "order by degree asc, stuid desc"; // append sorting clause} else if ($ queryType = "papers") {$ sqlsmt = "s Elect authors, title, jorc, vap, level, status from $ queryType where "; foreach ($ keywords as $ keyword) {$ sqlsmt. = "concat (authors, title, jorc, vap, level, status) like '% $ keyword %' or" ;}$ sqlsmt = substr ($ sqlsmt, 0, strlen ($ sqlsmt)-3); $ sqlsmt. = "order by level desc, vap desc";} else if ($ queryType = "softwares") {$ sqlsmt = "select authors, title, num, date from $ queryType where "; foreach ($ keywords as $ keyword ){ $ Sqlsmt. = "concat (authors, title, num, date) like '% $ keyword %' or" ;}$ sqlsmt = substr ($ sqlsmt, 0, strlen ($ sqlsmt) -3); $ sqlsmt. = "order by date desc";} else if ($ queryType = "patents") {$ sqlsmt = "select authors, title, acceptDate, acceptNum, authorizeDate, authorizeNum from $ queryType where "; foreach ($ keywords as $ keyword) {$ sqlsmt. = "concat (authors, title, acceptDate, acceptNum, authorizeDate, authorizeNum) like' % $ Keyword % 'or ";}$ sqlsmt = substr ($ sqlsmt, 0, strlen ($ sqlsmt)-3); $ sqlsmt. = "order by acceptDate desc" ;}$ myrs = mysql_query ($ sqlsmt); // execute the query if (mysql_num_rows ($ myrs) = 0) // if the result is null, return 'no' {echo (json_encode ("no "));} else // otherwise, the returned results in json format {while ($ row = mysql_fetch_array ($ myrs) {$ temp [] = $ row ;} echo (json_encode ($ temp) ;}?>

Running result:

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.