How can the readFromDB() function be optimized for better performance in the given scenario?
The readFromDB() function can be optimized for better performance by minimizing the number of database queries being executed. One way to achieve this is by fetching all the required data in a single query instead of making multiple queries for each item. This can be done by using a SQL query that retrieves all the necessary data at once.
// Optimized readFromDB() function
function readFromDB() {
$db = new PDO("mysql:host=localhost;dbname=myDB", "username", "password");
$query = "SELECT * FROM items";
$stmt = $db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}