Home > Article > Backend Development > How to use PHP and UniApp to implement fuzzy matching and query of data
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)
);
0a76a3268a4d7ec6835e6bd221ff8198connect_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.
d477f9ce7bf77f53fbcf36bec1b69b7a
<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>
21c97d3a051048b8e55e3c8f199a54b2
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!