mariam Posted June 15, 2011 Share Posted June 15, 2011 i am trying to plot a graph taking values from a database table in my sql. follwing is the code <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="mehreen"; // Database name include "db.php"; mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); $qt=mysql_query("select * from gd_graph"); header ("Content-type: image/jpg"); $x_gap=40; // The gap between each point in y axis $x_max=$x_gap*13; // Maximum width of the graph or horizontal axis $y_max=250; // Maximum hight of the graph or vertical axis // Above two variables will be used to create a canvas of the image// $im = @ImageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream"); $background_color = ImageColorAllocate ($im, 234, 234, 234); $text_color = ImageColorAllocate ($im, 233, 14, 91); $graph_color = ImageColorAllocate ($im,25,25,25); $x1=0; $y1=0; $first_one="yes"; while($nt=mysql_fetch_array($qt)){ //echo "$nt[month], $nt[sales]"; $x2=$x1+$x_gap; // Shifting in X axis $y2=$y_max-$nt[sales]; // Coordinate of Y axis ImageString($im,2,$x2,$y2,$nt[month],$graph_color); //Line above is to print month names on the graph if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0 imageline ($im,$x1, $y1,$x2,$y2,$text_color); // Drawing the line between two points } $x1=$x2; // Storing the value for next draw $y1=$y2; $first_one="no"; // Now flag is set to allow the drawing } ImageJPEG ($im); ?> the error occured is: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\drawgraph.php:2) in C:\xampp\htdocs\drawgraph.php on line 15 Quote Link to comment https://forums.phpfreaks.com/topic/239411-real-time-graph-plotting/ Share on other sites More sharing options...
gristoi Posted June 15, 2011 Share Posted June 15, 2011 its your header causing the issue: header ("Content-type: image/jpg"); check for whitespace or something else echoing out above this line Quote Link to comment https://forums.phpfreaks.com/topic/239411-real-time-graph-plotting/#findComment-1229918 Share on other sites More sharing options...
mariam Posted June 15, 2011 Author Share Posted June 15, 2011 please could you elaborate the meaning of white spaces before line? Quote Link to comment https://forums.phpfreaks.com/topic/239411-real-time-graph-plotting/#findComment-1229951 Share on other sites More sharing options...
gristoi Posted June 15, 2011 Share Posted June 15, 2011 The headers already sent error can be caused having white space (extra blank spaces before or after the opening and closing PHP tags. (<?php ?>) all headers should generally be placed at the top of the page to avoid this. Also if you have included another file that is declaring a header before line 15 then that will cause a problem. Quote Link to comment https://forums.phpfreaks.com/topic/239411-real-time-graph-plotting/#findComment-1229954 Share on other sites More sharing options...
pepo Posted June 15, 2011 Share Posted June 15, 2011 first make sure there is no white spaces around the opening and closing php tags then place this line header ("Content-type: image/jpg"); immediately after the opening php tag <?php Quote Link to comment https://forums.phpfreaks.com/topic/239411-real-time-graph-plotting/#findComment-1229965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.