How can an array be populated with database entries in PHP?
To populate an array with database entries in PHP, you can use a database query to retrieve the data and then loop through the results to add them to the array. This can be achieved using PHP's PDO extension to connect to the database and execute the query.
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare the query
$stmt = $pdo->prepare('SELECT * FROM mytable');
// Execute the query
$stmt->execute();
// Populate the array with database entries
$data = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data[] = $row;
}
// Output the populated array
print_r($data);