how to create bar chart in php and mysql Example?

Today, We want to share with you how to create bar chart in php and mysql.In this post we will show you PHP Chart Data from Database, hear for Creating a Bar Chart using mySQL in PHP we will give you demo and example for implement.In this post, we will learn about how to create bar chart in php and mysql? with an example.

Php Google Bar Chart Tutorial

Step 1 : Create Table

CREATE TABLE `products` (
  `name` varchar(70) NOT NULL,
  `number` int(11) NOT NULL,

Step 2 : Configuration


Step 3 : HTML & PHP Code



Generate Bar Chart in PHP

How to Create a Bar Graph using PHP/MySQLi and ChartJS?

To create a bar graph using PHP/MySQLi and ChartJS, you can follow these steps:

Create a database table to store the data for your bar graph. The table should have columns for the label and value of each data point.

Populate the database table with some sample data.

Create a PHP script that queries the database to retrieve the data for the bar graph.

// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Query the database to retrieve the data for the bar graph
$result = mysqli_query($conn, "SELECT label, value FROM bar_graph_data");

// Create an array to store the label and value of each data point
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;

Include the ChartJS library in your HTML file.

Create a canvas element to display the bar graph.

Create a JavaScript block that retrieves the data from your PHP script and creates the bar graph using ChartJS.

// Retrieve the data for the bar graph from the PHP script
    url: "bar_graph_data.php",
    method: "GET",
    success: function(data) {
        var labels = [];
        var values = [];

        // Parse the data returned by the PHP script
        data = JSON.parse(data);
        data.forEach(function(item) {

        // Create a new bar chart using ChartJS
        var ctx = document.getElementById("barChart");
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Bar Graph',
                    data: values,
                    backgroundColor: 'rgba(255, 99, 132, 0.2)',
                    borderColor: 'rgba(255, 99, 132, 1)',
                    borderWidth: 1
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true

In this JavaScript block, we use jQuery’s $.ajax() method to retrieve the data for the bar graph from our PHP script. We then parse the data using JSON.parse() and use the resulting arrays to create a new bar chart using ChartJS. The options for the chart, such as the chart type, data, and colors, are specified in the myChart object.

Once you have completed these steps, you should be able to see your bar graph on your webpage.

I hope you get an idea about How to Create a Bar Graph in PHP/MySQLi.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment