What are the best practices for implementing a full-text search with MySQL in PHP?
Implementing a full-text search with MySQL in PHP involves using the MATCH() AGAINST() syntax in your SQL query to search for relevant results based on a given search term. It is important to properly sanitize user input to prevent SQL injection attacks and to optimize your database tables for full-text searching.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Sanitize user input
$searchTerm = $mysqli->real_escape_string($_GET['search']);
// Perform full-text search query
$query = "SELECT * FROM your_table WHERE MATCH(column_name) AGAINST('$searchTerm' IN BOOLEAN MODE)";
$result = $mysqli->query($query);
// Display search results
while($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
// Close database connection
$mysqli->close();