How to use PHP and UniApp to implement fuzzy matching and query of data
Introduction:
In many development projects, we often need to implement fuzzy matching and query functions for data. PHP and UniApp are commonly used development technologies. This article will introduce how to use PHP to write backend interfaces, and how to use these interfaces in UniApp to implement fuzzy matching and query of data.
1. PHP writing background interface
CREATE TABLE users (
id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255)
);
// Connect to the database
$conn = new mysqli("localhost", "username", "password", "database ");
// Check whether the connection is successful
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// Get query conditions
$keyword = $_POST["keyword"];
//Construct query statement
$sql = "SELECT * FROM users WHERE name LIKE '%$keyword%'";
// Execute query
$result = $conn->query($sql);
// Process query results
if ($result->num_rows > 0) {
// 将查询结果转换为关联数组 $rows = array(); while ($row = $result->fetch_assoc()) { $rows[] = $row; } // 返回查询结果 echo json_encode($rows);
} else {
// 返回空结果 echo json_encode(array());
}
// Close the database connection
$conn->close();
?>
Please note that this is just a simple example, and more complex query conditions and data processing logic may be required in actual development.
2. Use the backend interface to implement fuzzy matching and query in UniApp
export default {
data() { return { keyword: '', users: [] }; }, methods: { searchData() { uni.request({ url: '/api/search.php', method: 'POST', data: { keyword: this.keyword }, success: (res) => { this.users = res.data; }, fail: (err) => { console.error(err); } }); } }
}
In the above example, a data named "keyword" is defined through the data attribute, which is used to store the query keyword entered by the user. A method named "searchData" is defined through the method attribute, which is used to initiate query requests. When the query request returns successfully, the returned results are assigned to the "users" array in the data attribute.
<view> <input v-model="keyword" type="text" placeholder="请输入关键字"> <button @click="searchData">查询</button> <view v-for="(user, index) in users" :key="index"> <text>{{ user.name }}</text> <text>{{ user.email }}</text> </view> </view>
In the above example, use the "v-model" command to compare the value in the input box with the "keyword" in the data attribute Two-way binding. Call the "searchData" method in the "click" event of the query button to initiate a query request. Loop through each piece of data in the query results in the "v-for" directive.
Conclusion:
Using PHP and UniApp can easily realize the fuzzy matching and query functions of data. By writing the backend interface in PHP, you can apply the query conditions passed by the front end to the database query, and then return the query results to the front end for display through UniApp. I hope the examples in this article can help developers better understand and apply these technologies.
The above is the detailed content of How to use PHP and UniApp to implement fuzzy matching and query of data. For more information, please follow other related articles on the PHP Chinese website!