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);
?> 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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Ñ

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.