Jump to content

PHP ImageJpeg function will not work if I call mysql commands


Hardwarez

Recommended Posts

OK, Im out of hair time to ask.  ???

 

I am making a charting program that draws a chart from data in a myspl database.  However, I can not get my data!  Everything works fine until I do :

while ($row = mysql_fetch_array( $result ))

or

mysql_num_rows($result)

 

I get the error: The image “test.php” cannot be displayed, because it contains errors.

 

Here is the block of code so far:

<?php
header ("Content-type: image/png");
$im = @ImageCreate (820, 400)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 224, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);

$diagramWidth = 800;
$diagramHeight = 400;    
$daysToShow = 30;
$indexSpace=38; //38 pixels between index numbers
$numDiv=10; //number of index divisions



$colorAmmonia= imageColorAllocate($im, 145,150,0);
$colorNitrite= imageColorAllocate($im, 223,13,223);
$colorPh= imageColorAllocate($im, 153,96,15);
$colorFeed= imageColorAllocate($im, 75,232,39);
$colorNitrate= imageColorAllocate($im, 240,0,0);
$colorTemp= imageColorAllocate($im, 39,39,232);
$colorTempAir= imageColorAllocate($im, 147,147,188);
$colorFish= imageColorAllocate($im, 155,121,49);
$colorFilter= imageColorAllocate($im, 230,200,200);
$colorFrame = ImageColorAllocate ($im,0,0,0);


// draw graph text---------------------------------------------------------
imageString($im, 4, 0, $diagramHeight - 400,  "Ammonia", $colorAmmonia);
imageString($im, 4, 0, $diagramHeight - 385,  "Nitrite", $colorNitrite );
imageString($im, 4, 0, $diagramHeight - 370,  "pH", $colorPh );
imageString($im, 4, 0, $diagramHeight - 355,  "Feed", $colorFeed );

imageString($im, 4, 740, $diagramHeight - 400,  "Nitrate", $colorNitrate );
imageString($im, 4, 740, $diagramHeight - 385,  "Water Temp", $colorTemp );
imageString($im, 4, 740, $diagramHeight - 370,  "Air Temp", $colorTempAir );
imageString($im, 4, 740, $diagramHeight - 355,  "FishWeight", $colorFish );
imageString($im, 4, 60, $diagramHeight - 28,  "0", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 58,  "1", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 96,  "2", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 134,  "3", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 172,  "4", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 210,  "5", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 248,  "6", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 286,  "7", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 324,  "8", $colorFrame );
imageString($im, 4, 60, $diagramHeight - 362 ,  "9", $colorFrame );

imageString($im, 4, 714, $diagramHeight - 38,  "0", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 76,  "47", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 114,  "62", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 152,  "77", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 190,  "92", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 228,  "107", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 266,  "122", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 304,  "137", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 342,  "152", $colorFrame );
imageString($im, 4, 714, $diagramHeight - 380,  "167", $colorFrame );

imageString($im, 4, 52, $diagramHeight -20 ,  "Jan 1", $colorFrame );

//draw graph frame--------------------------
imageline ($im,72,0,72,380,$colorFrame);
imageline ($im,72,380,710,380,$colorFrame);
imageline ($im,710,380,710,0,$colorFrame);

//read database---------------------------------------------------------------------------
$user		=	"x";
$password	=	"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$database	=	"datatracker";
$host		=	"localhost";
$link = mysql_pconnect($host, $user, $password);
mysql_select_db("aptracker") or die(mysql_error());

$user=$_GET['user'];
if ($_GET['days']){
$daysToShow=$_GET['days']; //default is past 30 days unless specified
}
$query = "SELECT * FROM data WHERE user='$user' ORDER BY date LIMIT '$daysToShow'";
$result = mysql_query($query);
//$number = mysql_num_rows($result);
$i=0;
while ($row = mysql_fetch_array( $result )) {  // ==============DISPLAY ALL RESULTS
	++$i;

	$date[i]=$row['date'];
	$ph[i]=$row['ph'];
	$waterTemp[i]=$row['waterTemp'];
	$airtemp[i]=$row['airTemp'];
	$ammoni[i]=$row['ammonia'];
	$nitrite[i]=$row['nitrite'];
	$nitrate[i]=$row['nitrate'];
	$feed[i]=$row['feed'];
	$fish[i]=$row['fish'];
	$filter[i]=$row['filter'] ;

}		//end WHILE LOOP



ImageJpeg ($im);
?> 

 

 

Usually what that means is that there is text being outputted before the image is created.

 

Try moving the header() statement before the imagejpeg() function and see what happens. It could be a mysql error and its spitting the error msg out to the screen which is throwing the imagejpeg function.

If you comment out the header() and the ImageJpeg() functions does the script output anything odd? it should technically just display a blank page with those two lines commented out if everything else is right.

If I comment out the header() then I can see the image data and it is infact starting with a sql error here are the first 8 lines of the data:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in test.php on line 83
ÿØÿà�JFIF������ÿþ�>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛ�C�    $.' ",#(7),01444'9=82<.342ÿÛ�C  2!!22222222222222222222222222222222222222222222222222ÿÀ�4"�ÿÄ����������� ÿÄ�µ���}�!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ�������� ÿÄ�µ��w�!1AQaq"2B‘¡±Á #3RðbrÑ

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.