How can PHP be used to enhance a text search function in a database?
When searching for text in a database, PHP can be used to enhance the search functionality by allowing for more complex search queries, such as searching for partial matches, case-insensitive searches, or searching multiple columns simultaneously.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Search query
$search_term = $_GET['search_term'];
$sql = "SELECT * FROM table WHERE column LIKE '%$search_term%'";
// Execute the query
$result = $conn->query($sql);
// Display search results
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results found";
}
// Close the database connection
$conn->close();