How can you ensure that a PHP function returns values from multiple rows in a database query?

When a PHP function is used to query a database and retrieve multiple rows of data, the function should return an array of the results. This can be achieved by looping through the query results and adding each row to an array. Finally, the function should return this array of results.

function getMultipleRowsFromDatabase() {
    // Connect to the database
    $conn = new mysqli("localhost", "username", "password", "database");

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

    // Query to retrieve multiple rows from the database
    $sql = "SELECT * FROM table_name";
    $result = $conn->query($sql);

    // Check if there are rows in the result
    if ($result->num_rows > 0) {
        $rows = array();
        // Loop through each row and add it to the array
        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        // Close the database connection
        $conn->close();
        // Return the array of rows
        return $rows;
    } else {
        // Close the database connection
        $conn->close();
        // Return an empty array if no rows are found
        return array();
    }
}

// Call the function to retrieve multiple rows from the database
$multipleRows = getMultipleRowsFromDatabase();

// Loop through the array of rows and do something with the data
foreach ($multipleRows as $row) {
    // Do something with each row
    echo $row['column_name'] . "<br>";
}