How can PHP be used in conjunction with MySQL to create a search form on a website?

To create a search form on a website using PHP and MySQL, you can start by creating a form in HTML that allows users to input their search query. When the form is submitted, the PHP code will retrieve the search query, sanitize it to prevent SQL injection, and then query the MySQL database for relevant results. The results can then be displayed on the website.

<?php
// Check if the form is submitted
if(isset($_POST['search'])) {
    // Get the search query from the form
    $search = $_POST['search'];
    
    // Sanitize the search query to prevent SQL injection
    $search = mysqli_real_escape_string($connection, $search);
    
    // Query the database for relevant results
    $query = "SELECT * FROM table_name WHERE column_name LIKE '%$search%'";
    $result = mysqli_query($connection, $query);
    
    // Display the results
    while($row = mysqli_fetch_assoc($result)) {
        echo $row['column_name'] . "<br>";
    }
}
?>

<form method="post" action="">
    <input type="text" name="search">
    <input type="submit" value="Search">
</form>