pie chart in php with database – How to Make Pie Chart in Php and Mysql?

pie chart in php with database – here you can learn to Create Dynamic Pie Chart using Google API, PHP and MySQL example with demo. Steps to create a pie chart. So lets create the database by using following queries.

pie chart in php with database

Create Dynamic Pie Chart using Google API, PHP and MySQL – Dynamic Pie chart Using the Google Chart tools we will create pie chart in PHP with MySQL.

create a Pie Chart of the data using the Google API, PHP and MySQL

The following SQL creates players table which consist of id, code, players and ranks columns.

  `code` varchar(100) NOT NULL,
  `players` varchar(100) NOT NULL,
  `ranks` int(11) NOT NULL,
  PRIMARY KEY (`id`)


INSERT INTO `players` (`id`, `code`, `players`, `ranks`) VALUES
(1, 'VK', 'virat kohali', 6),
(2, 'RS', 'rohit shamra', 16),
(3, 'KL', 'k l rahul', 14),
(4, 'ST', 'sachin tendulkar', 8),
(5, 'YV', 'yuvraj', 6);

Free Live Chat for Any Issue

first of all call ajax function to get players data, The DataTable object is used to hold the data passed into a visualization, To render the pie chart, load the visualization api and Set a callback to run when the Google Visualization API is loaded.

Also Read This πŸ‘‰   jQuery Check if element exists or not

Pie Chart using data from MySQL table by PHP

1. index.php

<!DOCTYPE html>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            function drawChart() {

                var jsonData = $.ajax({
                    url: "getData.php",
                    dataType: "json",
                    async: false
                var data = new google.visualization.DataTable(jsonData);

                var chart = new google.visualization.PieChart(document.getElementById('player_info'));
                chart.draw(data, {width: 400, height: 240});
            google.charts.load('current', {'packages':['corechart']});

        <div id="player_info"></div>

2. getData.php

MySQL database connection code

$connection = mysqli_connect("host_name","user_name","password","db_name") or die("Error " . mysqli_error($connection));

$sql = "SELECT * FROM players";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

$array = array();
$i = 0;
while($row = mysqli_fetch_assoc($result))
    $orgname = $row['players'];
    $count = $row['ranks'];
    $array['cols'][] = array('type' => 'string'); 
    $array['rows'][] = array('c' => array( array('v'=> $orgname), array('v'=>(int)$count)) );
$data = json_encode($array);
echo $data;

Don’t Miss : how to create bar chart in php and mysql

I hope you get an idea about pie chart in php with database.
I would like to have feedback on my infinityknow.com 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.