How can one optimize the performance of PHP scripts that involve database queries, especially when retrieving and displaying data from a MySQL database?

To optimize the performance of PHP scripts involving database queries, especially when retrieving and displaying data from a MySQL database, one should consider using proper indexing on the database tables, minimizing the number of queries by using JOINs or subqueries, caching query results where appropriate, and using prepared statements to prevent SQL injection attacks.

// Example of optimizing PHP script with MySQL database queries
// Using proper indexing and minimizing queries

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

// Query to retrieve data with proper indexing
$query = "SELECT * FROM users WHERE status = 'active' ORDER BY registration_date DESC";

// Execute the query
$result = $mysqli->query($query);

// Loop through the results and display data
while ($row = $result->fetch_assoc()) {
    echo "Name: " . $row['name'] . "<br>";
    echo "Email: " . $row['email'] . "<br>";
    echo "Registration Date: " . $row['registration_date'] . "<br><br>";
}

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