How can AJAX be utilized to improve the user experience when accessing data from multiple databases in PHP?

When accessing data from multiple databases in PHP, the user experience can be improved by using AJAX to fetch data asynchronously without reloading the entire page. This can make the application more responsive and efficient, as only the necessary data is fetched from the server when needed.

// PHP code snippet utilizing AJAX to improve user experience when accessing data from multiple databases

// Create a PHP file (e.g., fetchData.php) to handle AJAX requests
// fetchData.php
<?php
// Include necessary database connection files
require_once('db1_connection.php');
require_once('db2_connection.php');

// Fetch data from multiple databases
$data1 = fetchDataFromDB1();
$data2 = fetchDataFromDB2();

// Return the fetched data as JSON
echo json_encode(array('data1' => $data1, 'data2' => $data2));
?>
```

```javascript
// JavaScript code to make an AJAX request and update the UI with the fetched data
// Assuming jQuery is included in the project

$.ajax({
  url: 'fetchData.php',
  type: 'GET',
  dataType: 'json',
  success: function(response) {
    // Update the UI with the fetched data
    $('#data1').html(response.data1);
    $('#data2').html(response.data2);
  },
  error: function(xhr, status, error) {
    console.log('Error fetching data: ' + error);
  }
});