# Advanced Graphs and Charts for PHP

- Graph Home
- Pricing / License Options
- Online Demos
- Documentation
- Tutorials

## Database Interface Script

Here the graphing software is instructed to dynamically call at runtime a custom data script.

This method allows you to create your own PHP Scripts to produce the data to be graphed. This method can be used to acquire data from a database or any other data source to which your PHP environment has access.

The graph above is produced using the "Data Interface Script" method with the following IMG tag:-

<img src="../../graph/vertical-bar-graph.php? data=../sampleApplication/dataQueries/dataInterfaceScript.php& config=../sampleApplication/graphConfig/salesByMonthConfig.txt" width="700" height="400" />

The graph data is produced by the interface script and read by the graphing software at runtime:-

/sampleApplication/dataQueries/dataInterfaceScript.php /* * perform the query for each series of data */ $dataLines = array(); /* * Blue Robots */ $sql = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '1' GROUP BY saleMonth ORDER BY saleMonth "; $result = mysql_query($sql, $dbLink); $dataNum = 1; while ( $row = mysql_fetch_array($result,MYSQL_ASSOC) ) { $dataLines[] = "data".$dataNum."series1: ".$row["monthAmount"]; $dataNum++; } mysql_free_result($result); /* * Green Robots */ $sql = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '2' GROUP BY saleMonth ORDER BY saleMonth "; $result = mysql_query($sql, $dbLink); $dataNum = 1; while ( $row = mysql_fetch_array($result,MYSQL_ASSOC) ) { $dataLines[] = "data".$dataNum."series2: ".$row["monthAmount"]; $dataNum++; } mysql_free_result($result); /* * Red Robots */ $sql = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '3' GROUP BY saleMonth ORDER BY saleMonth "; $result = mysql_query($sql, $dbLink); $dataNum = 1; while ( $row = mysql_fetch_array($result,MYSQL_ASSOC) ) { $dataLines[] = "data".$dataNum."series3: ".$row["monthAmount"]; $dataNum++; } mysql_free_result($result); /* * Yellow Robots */ $sql = "SELECT SUM(amount) AS monthAmount FROM salesByMonth WHERE saleYear=2008 AND product_id = '4' GROUP BY saleMonth ORDER BY saleMonth "; $result = mysql_query($sql, $dbLink); $dataNum = 1; while ( $row = mysql_fetch_array($result,MYSQL_ASSOC) ) { $dataLines[] = "data".$dataNum."series4: ".$row["monthAmount"]; $dataNum++; } mysql_free_result($result); /* * Output the data lines */ foreach ($dataLines as $dataLine) { print $dataLine . PHP_EOL; }

With this method the graphing software calls the interface script at runtime and reads the output of the script in the same way that it would read the data from a flat file. This method allows data to be dynamically produced and plotted in real-time.

The settings and styles are set from the information contained in the file:-

/sampleApplication/graphConfig/salesByMonthConfig.txt

If the graph above does not display then see the Troubleshooting guide for details on how to resolve the problem.

If the graph above does display but no data is shown then the most likely cause is that the graph is having trouble connecting to the database server. Ensure that the 'Sample Application' has been set up with the correct DB User credentials.

If you have not yet set up the Sample Application then you can do so here.

Package Index

Documentation

Troubleshooting Guide

Support

#### Examples

- Graphing Data from files
- Data from external scripts
- Custom data function
- Utilising request parameters
- Plotting data from a database
- Database interface scripts
- Multiple Scales, text and images
- Graphing CSV files

#### Basic Chart Styles

- Pie Charts
- X-Y Scatter Graphs
- Bubble Charts
- Area Graphs
- Line Graphs

#### Bar Graphs

- Vertical Bar Graphs
- Stacked Vertical Bar Graphs
- Horizontal Bar Graphs
- Stacked Horizontal Bar Graphs

#### Cylinder Charts

- Vertical Cylinder Graphs
- Stacked Vertical Cylinder Graphs
- Horizontal Cylinder Graphs
- Stacked Horizontal Cylinder Graphs

#### Combination Charts

- Area and Stacked Vertical Bar Graph
- Area and Vertical Bar Graph
- Line and Stacked Vertical Bar Graph
- Line and Vertical Bar Graph