Home  >  Article  >  Backend Development  >  How to implement the function of displaying query results while typing in PHP

How to implement the function of displaying query results while typing in PHP

PHPz
PHPzOriginal
2023-04-11 09:16:16807browse

PHP is a commonly used server-side scripting language, which can help us process data and generate HTML pages. In many web applications, querying the database is a common requirement, because this allows users to obtain the required information by entering keywords.

Here, I will introduce to you how to use PHP to write a piece of code to display the query results while typing. In this way, when users enter keywords, matching results will be displayed in real time, making the search process more convenient and efficient.

First, we need to connect to the database, you can use PHP's built-in mysqli or PDO object. In this article, I will use mysqli.

//连接数据库
$con = mysqli_connect("localhost","username","password","database");
if (!$con){
    die('Could not connect: ' . mysqli_error());
}

Next, we need to capture the keywords entered by the user, which can be achieved by using the $_POST or $_GET array. Here I will use the $_POST array.

//获取用户输入
$keyword = $_POST["keyword"];

Next, we need to query the database based on the keywords entered by the user and output the results to the page. In order to achieve the effect of displaying while inputting, we need to use AJAX technology.

AJAX is a technology for asynchronous communication between the browser and the server, which can update data without refreshing the page. In this example, we will use jQuery's AJAX method to achieve this.

//使用AJAX来查询数据库
$.ajax({
    type: "POST",
    url: "search.php",
    data: { keyword: keyword },
    success: function(data){
        $("#results").html(data); //将查询结果输出到页面上
    }
});

The above code passes the keywords entered by the user to the search.php file for processing, and outputs the results to the page.

In the search.php file, we need to retrieve the database and return matching results. Here is only a simple example:

//查询数据库
$sql = "SELECT * FROM table WHERE column LIKE '%".$keyword."%'";
$result = mysqli_query($con,$sql);

//输出查询结果
while($row = mysqli_fetch_array($result)){
    echo $row['column']."<br>";
}

The above code will query the database for rows matching the keyword in the table with the column name column, and output them to the page.

Finally, we need to add a text box and a label to the page to display the query results.

<input type="text" name="search" id="search" onkeyup="search()">
<div id="results"></div>

Now, we have completed the function of displaying query results while typing. Users only need to enter keywords, and the page will display matching results in real time. In this way, we can greatly improve the user experience and search efficiency of web applications.

In short, displaying query results while inputting in PHP requires the use of AJAX technology and the reasonable use of SQL statements to achieve data retrieval. Hope this article is helpful to you.

The above is the detailed content of How to implement the function of displaying query results while typing in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn