Are there any best practices for storing database entries in arrays in PHP?

When storing database entries in arrays in PHP, it is best practice to fetch the data from the database using a query and then store the results in an array. This allows for easier manipulation and access to the data. It is also important to properly sanitize and validate the data before storing it in an array to prevent any security vulnerabilities.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// Fetch data from the database
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

// Store the data in an array
$data = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}

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

// Now you can access and manipulate the data stored in the array