What is the best practice for storing SQL query results in arrays for later use in PHP?

When storing SQL query results in arrays for later use in PHP, it is best practice to fetch the results using a fetch method such as fetch_assoc() or fetch_all() to store them in an array. This ensures that the data is easily accessible and can be manipulated as needed in the PHP code.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Perform a query
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

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

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

// Now $data contains the SQL query results in an array for later use