How can developers efficiently connect a MySQL database to a jQuery Mobile application using PHP?

Developers can efficiently connect a MySQL database to a jQuery Mobile application using PHP by creating a PHP script that connects to the database, retrieves the data, and encodes it into JSON format. This JSON data can then be fetched by the jQuery Mobile application using AJAX requests.

<?php

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database_name");

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

// Fetch data from MySQL database
$query = "SELECT * FROM table_name";
$result = $mysqli->query($query);

// Encode data into JSON format
$data = array();
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

echo json_encode($data);

// Close connection
$mysqli->close();

?>