How can PHP interact with MySQL to retrieve and display a list of street names associated with a given postal code in real-time on a single webpage?

To retrieve and display a list of street names associated with a given postal code in real-time on a single webpage, you can use PHP to interact with MySQL database. You can create a form where users can input a postal code, then use AJAX to send the postal code to a PHP script that queries the database for street names associated with that postal code. The PHP script can then return the list of street names back to the webpage for display.

<?php
// Establish a connection to the 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 the postal code from the AJAX request
$postal_code = $_GET['postal_code'];

// Query the database for street names associated with the postal code
$sql = "SELECT street_name FROM streets WHERE postal_code = '$postal_code'";
$result = $conn->query($sql);

// Fetch and display the street names
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row['street_name'] . "<br>";
    }
} else {
    echo "No streets found for this postal code";
}

// Close the database connection
$conn->close();
?>