What are the advantages and disadvantages of using AJAX to display content from a database in PHP?

Issue: When displaying content from a database in PHP, using AJAX can provide a smoother user experience by loading content dynamically without refreshing the entire page. However, it can also add complexity to the code and may require additional server-side handling to properly retrieve and display the data.

<?php
// PHP code to retrieve data from database and return as JSON
// This code snippet assumes you have a database connection established

// Retrieve data from database
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

// Fetch data as an associative array
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
    $rows[] = $row;
}

// Return data as JSON
echo json_encode($rows);
?>