Are there any specific best practices for integrating jQuery and Ajax in PHP for automatic data reloading?

When integrating jQuery and Ajax in PHP for automatic data reloading, it is important to follow best practices to ensure efficient and reliable functionality. One common approach is to use setInterval() in jQuery to make periodic Ajax calls to a PHP script that retrieves updated data from a database and returns it to the client-side. This allows for seamless automatic data reloading without the need for manual page refreshes.

// PHP script to fetch updated data from the database
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Query to fetch updated data
$sql = "SELECT * FROM your_table";
$result = mysqli_query($conn, $sql);

$data = array();

// Fetch data and store in an array
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
}

// Return data as JSON
echo json_encode($data);

// Close connection
mysqli_close($conn);
?>