How can PHP be utilized to search for specific terms in a MySQL database?

To search for specific terms in a MySQL database using PHP, you can use the SELECT query with the WHERE clause to filter the results based on the search term. You can use PHP variables to store the search term input by the user and then dynamically construct the SQL query to search for that term in the database.

<?php
// Connect to MySQL 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 search term from user input
$searchTerm = $_POST['searchTerm'];

// Construct SQL query to search for term in database
$sql = "SELECT * FROM table_name WHERE column_name LIKE '%$searchTerm%'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results found";
}

$conn->close();
?>