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` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `name` varchar(70) NOT NULL, `number` int(11) NOT NULL, `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1
Step 2 : Configuration
config.php
<?php $host = "localhost"; $user = "jdksjjs"; $password = "[email protected]"; $dbname = "pakainfo"; $con = mysqli_connect($host, $user, $password,$dbname); if (!$con) { die("Connection failed: " . mysqli_connect_error()); } $chartQuery = "SELECT * FROM products"; $chartQueryRecords = mysqli_query($con,$chartQuery); ?>
Step 3 : HTML & PHP Code
index.php
<?php include "config.php"; ?> <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <body style="text-align: center;"> <h2 class="text-center">Generate Bar Chart in PHP</h2> <div id="regions_div" style="width: 900px; height: 500px;"></div> <div class="text-center"> <h2>www.pakainfo.com</h2> </div> <script type="text/javascript"> google.charts.load('current', { 'packages':['imagebarchart'], 'mapsApiKey': 'Ldfdioko-8dfuDkjdf85d5fdfdff8-a7eZSW0jkLKFIO' }); google.charts.setOnLoadCallback(drawRegionsMap); function drawRegionsMap() { var data = google.visualization.arrayToDataTable([ ['Year', 'Sales', 'Expenses'], <?php while($row = mysqli_fetch_assoc($chartQueryRecords)){ echo "['".$row['year']."',".$row['sales'].",".$row['expenses']."],"; } ?> ]); var options = { }; var chart = new google.visualization.ImageBarChart(document.getElementById('regions_div')); chart.draw(data, options); } </script> </head> </body> </html>
I hope you get an idea about How to Create a Bar Graph in PHP/MySQLi.
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.