JPowered.com

Graphing Tutorials - Dynamic MySQL Connection

 

Direct MySQL Connection

This tutorial demonstrates how to set the graph to dynamically pull the data directly from MySQL databases.

Prerequisites

The following tutorials assume you have already downloaded and installed the
PHP Advanced Graphs and Charts package.

The latest release can be downloaded here

Directory Structure

In this tutorial you will be create a web page and a couple of configuration files. For the purposes of this tutorial you should create a directory /graphtest/ directory under your web root directory.

This tutorial also assumes that you installed the graphing software directly under the web root
(e.g. /webRootFilePath/jpowered/graph/ )

Step 1 - Create a test MySQL Database

For the purpose of this tutorial the following MySQL database should be created.-

Now add the following data to the 'sales' table:-

Step 2 - Create the dbinfo.php file

So that the graph can connect to the database it requires certain pieces of information including a database username and password.

Create a file named dbinfo.php which contains the following:-

Replace [USER] and [PASSWORD] with valid logon details for your MySQL server.

Place the file on your web server in a directory /graphtest/ under your web root directory

Note: The above assumes that your MySQL server is running on the same machine as your Web Server. If this is not the case then you will need to replace "localhost:3306" with the correct route to your MySQL server.

The graphing software will execute each of the SELECT statements found in this file. The results of each query will be plotted as a series of data on the chart.

More information on the format and workings of this file can be found on the following page of the documentation:-
Database Information File »

Step 3 - Create the config file

Create a file named 'config.txt' and add the following contents to it. This file should then be placed in the /graphtest/ directory.

Notes: This config.txt file contains the parameters which determine certain aspects of the graph like colors and fonts. For the function and options of each parameter see the following page:-
Graph Parameters »

Step 4 - Create a web page and add the Graph to it

Add this HTML code to your page (replacing [www.YourDomain.com] with the actual domain of your web site and [URL FilePath] with the url of the web directory where your page resides)

Note:[Relative FilePath] should be replaced with a relative file path which is relative to the location of the line-graph.php file (it is NOT relative to the location of your web page).
For example if you place the dbinfo.php in a directory /graphtest/ then your dbinfo parameter should look like this:-

dbinfo=../../graphtest/dbinfo.php

Save the web page and load it up to your web server placing it in the /graphtest/ directory.

Final Step - View the page in a browser

Open your web browser and view the page. You should see a Line Graph with 18 points plotted (6 for each series).

 

 

 

Tutorials

Adding Graphs to Web Pages

Direct DB Access to MySQL DBs

Custom Data Function

Interface Scripts

Constructing configuration dynamically

Creating Date/Time Axis Labels

Graphing Data Directly from Spreadsheets

More

Documentation

Demo Index

Troubleshooting Guide

Support

Documentation

Configuration Options and Parameters

Supplying the Graph with Data

Sample Image Tags

Database Connections

Database Information method

Custom Data Function

Installation Help

Upgrade PHP

Upgrade GD

Log File

Allow URL Fopen