To implement search functionality with full-text search using PDO in PHP, follow these steps:
$dbhost = "your_host";
$dbname = "your_database";
$username = "your_username";
$password = "your_password";
try {
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$searchTerm = "your_search_term";
$searchQuery = "SELECT * FROM your_table WHERE MATCH(column_name) AGAINST(:searchTerm IN BOOLEAN MODE)";
Replace your_search_term
with the term you want to search for, your_table
with the table name, and column_name
with the column name that you want to search.
$stmt = $db->prepare($searchQuery);
$stmt->bindValue(':searchTerm', $searchTerm);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row) {
// Display the search results
echo $row['column_name'];
}
That's it! You have implemented search functionality with full-text search using PDO in PHP.