What are the best practices for handling and passing database records to generate diagrams in PHP?

When handling and passing database records to generate diagrams in PHP, it is best practice to retrieve the data from the database using SQL queries, store the results in an array, and then pass this array to a function that generates the desired diagram. This ensures separation of concerns and makes the code more modular and easier to maintain.

// Retrieve data from the database
$query = "SELECT * FROM your_table";
$result = mysqli_query($connection, $query);

// Store the results in an array
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// Pass the data array to a function that generates the diagram
generateDiagram($data);

function generateDiagram($data) {
    // Code to generate the diagram using the data array
}