How to generate charts and graphs in php from mysql database

In this tutorial I will show you how you can generate graphs and charts using PHP. And the data that it will graph will be from a mysql database.

There are lots of php classes which allows you to create graphs in php. But my favorite is libchart.

Its my favorite, because it worked for me the first time I used it.

Here’s an example from the project that I’m currently working on:


I have just began using libchart so, the graphs looks like the default graphs that you can find in the libchart site.

What you will have to do first is to download libchart.

After downloading, you extract it with any archive extracting software. I recommend pea zip for this. Pea zip because it has good-looking interface and it supports many archive formats.

Extract it into your project folder. If you’re using wamp. Then your web accessible directory will be ‘www’ folder. You can go there by left-clicking on the wamp icon on the system tray and select ‘www directory’.

Go to your project folder. And copy the extracted folder inside.

So for example if my project folder is named ‘pos’. I’ll copy it inside the folder ‘pos’ and it will now look like this:


If you open up libchart folder. It must look like this:


Now, open up your notepad++ or any text-editing application. And examine the code below:


including the libchart.php file is the most important thing in here. Because without it you won’t see any output.

This specific line of code, is a loop. It loops through all the records in the database that matches the provide query:


That’s why my example above has all the same color, which is blue. Because its just looping. I’m still in the process of discovering how can I change the colors.

Important notes:

      • There must be a folder named ‘generated’ in the directory where you are calling libchart. So if ‘example.php’ is calling libchart, there must be a folder named ‘generated’ on the same directory.
      • If you can’t make it work, just try using the demo’s first before doing anything. Or you may also copy the demo and try to understand it. Base what you will do on the code that I have provided above. Its just easy.
      • Be sure you already know the basics of connecting to a mysql database via php. Things like mysql_connect and mysql_select_db should be placed before the query.

That’s all, its up for you to examine and understand the code that I provided so that you can come up with an output, thanks for reading.

1 thought on “How to generate charts and graphs in php from mysql database

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s