How can JavaScript and jQuery be effectively utilized to achieve real-time price updates without page reloads in a PHP script?

To achieve real-time price updates without page reloads in a PHP script, JavaScript and jQuery can be utilized. The JavaScript code can make AJAX requests to a PHP script that fetches updated price data from a database. The jQuery library can be used to handle the AJAX requests and update the price on the webpage dynamically without refreshing the entire page.

<?php
// PHP script to fetch updated price data from a database

// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

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

// Query to fetch updated price data
$sql = "SELECT price FROM products WHERE id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["price"];
    }
} else {
    echo "0 results";
}

$conn->close();
?>