PHPGraphLib Tutorial #2: MYSQL and PHPGraphLib
Overview
In this example, we are going to show a simple example of how to use PHPGraphLib with MYSQL.
Include the source file in a new script and create the PHPGraphLib Object
Include the source PHPGraphLib class file at the top of your stand
alone php script. This script will only include the PHP calls to create
your graph. Next, create a PHPGraphLib object.
|
<?php
include ( "phpgraphlib.php" );
$graph = new PHPGraphLib(550,350);
|
Add data from MYSQL
We are going to query our database, add that data to an array, and
then pass that information to PHPGraphLib. The sales count will be used
as y-axis values, and the sales group name will be used as the x -axis
values. See
http://www.php.net/mysql
for more information on how to connect to your MYSQL server with PHP.
You will need to change the system dependent variables, such as
username, password, database, and table name when using this example.
|
$link = mysql_connect( 'localhost' , 'username' , 'password' )
or die ( 'Could not connect: ' . mysql_error());
mysql_select_db( 'salestracking' ) or die ( 'Could not select database' );
$dataArray = array ();
$sql = "SELECT salesgroup, COUNT(*) AS 'count' FROM sales GROUP BY salesgroup" ;
$result = mysql_query( $sql ) or die ( 'Query failed: ' . mysql_error());
if ( $result ) {
while ( $row = mysql_fetch_assoc( $result )) {
$salesgroup = $row [ "salesgroup" ];
$count = $row [ "count" ];
$dataArray [ $salesgroup ]= $count ;
}
}
$graph ->addData( $dataArray );
$graph ->setTitle( "Sales by Group" );
$graph ->setGradient( "lime" , "green" );
$graph ->setBarOutlineColor( "black" );
$graph ->createGraph();
?>
|
Include the created script as the src of an image on the desired page
On the page you want the graph to appear, create an HTML image tag
and set the src="" to the location of the script you created above.
|
< html >
< h3 >This is where I want to display my graph</ h3 >
< img src = "mysql_graph_bar.php" />
</ html >
|
Here is the graph you just created:
For other examples, view the
Examples page
Hi there. Very cool website!! Man .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds also…I’m happy to locate a lot of useful information right here within the post. Thank you for sharing.
ReplyDeleteWeb development Company
Thanks for commenting and appreciation i always experiment new think if you have any new question you can ask by comment i always in here
ReplyDeletethanks
Bikash ranajn
img src="mysql_graph_bar.php"
ReplyDeleteThis tag is not showing me the output.
Please guide
Thanks for sharing. You can use our code of SMS API PHP for SMS notifications, signup messages, alert, reminder, and OTP. Make sure you have read developer guide that helps you to do SMS API integration into websites/applications/software in this language.
ReplyDelete