Are there any best practices for adding elements to an array from a database in PHP?

When adding elements to an array from a database in PHP, it is best practice to fetch the data from the database using a query, loop through the results, and add each row to the array. This ensures that the array contains all the necessary data from the database.

// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');

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

// Query to fetch data from the database
$query = "SELECT * FROM table_name";
$result = $connection->query($query);

// Create an empty array to store the data
$dataArray = array();

// Loop through the results and add each row to the array
while ($row = $result->fetch_assoc()) {
    $dataArray[] = $row;
}

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

// Print the array to see the data
print_r($dataArray);