How can one efficiently retrieve and store individual database entries into a multidimensional array in PHP?
To efficiently retrieve and store individual database entries into a multidimensional array in PHP, you can use a loop to fetch each row from the database result set and store it as an array within the multidimensional array.
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
// Prepare and execute the query
$stmt = $pdo->query("SELECT * FROM table_name");
$results = array();
// Fetch each row and store it in the multidimensional array
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$results[] = $row;
}
// Print the multidimensional array
print_r($results);