What are some best practices for handling SQL data retrieval and conversion to PHP arrays for graph plotting?

When retrieving data from a SQL database for graph plotting in PHP, it is best practice to fetch the data as an associative array using functions like mysqli_fetch_assoc(). This allows for easy conversion to PHP arrays for graph plotting. Additionally, it is important to properly handle any data formatting or type conversions needed before passing the data to the graph plotting library.

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

// Query to retrieve data
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Fetch data as associative array
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// Close the connection
mysqli_close($connection);

// Now $data is a PHP array ready for graph plotting