How can PHP be used to implement a search function in a database?

To implement a search function in a database using PHP, you can use SQL queries to search for specific data based on user input. The user input can be passed through a form and then used in the SQL query to retrieve relevant data from the database. The search results can then be displayed to the user on a webpage.

<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Get user input from a form
$searchTerm = $_POST['searchTerm'];

// Create a SQL query to search for data in the database
$sql = "SELECT * FROM table_name WHERE column_name LIKE '%$searchTerm%'";

$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 connection
$conn->close();
?>