<?phprequire("../Comm/Conn.php"); //从数据库读取数据部分
$result = mysqli_query($mysql, "select count(*) as nega from message where attitude=-1");
$row = mysqli_fetch_assoc($result);
$x=(int)$row['nega'];
$result = mysqli_query($mysql, "select count(*) as zero from message where attitude=0");
$row = mysqli_fetch_assoc($result);
$y=(int)$row['zero'];
$result = mysqli_query($mysql, "select count(*) as posi from message where attitude=1");
$row = mysqli_fetch_assoc($result);
$z=(int)$row['posi'];
mysqli_free_result($result);
mysqli_close($mysql);
$total=$x+$y+$z; //生成统计图部分
$height=200;
$width=200;$y1=$height-intval($x/$total*$height,10);
$y2=$height-intval($y/$total*$height,10);
$y3=$height-intval($z/$total*$height,10); $im=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($im,255,255,255);
$black=imagecolorallocate($im,0,0,0);
$red=imagecolorallocate($im,255,0,0);
$blue=imagecolorallocate($im,0,0,255);
$green=imagecolorallocate($im,0,255,0);imagefill($im,0,0,$white);
imagerectangle($im,0,0,$width-1,$height-1,$black);
imagefilledrectangle($im,$width/10,$y1,3*$width/10,$height-2,$red);
imagefilledrectangle($im,4*$width/10,$y2,6*$width/10,$height-2,$blue);
imagefilledrectangle($im,7*$width/10,$y3,9*$width/10,$height-2,$green);imagestring($im,5,30,180,$x,$white);
imagestring($im,5,90,180,$y,$white);
imagestring($im,5,150,180,$z,$red);Header('Content-type:image/png');
imagepng($im);
imagedestroy($im);
?>
$result = mysqli_query($mysql, "select count(*) as nega from message where attitude=-1");
$row = mysqli_fetch_assoc($result);
$x=(int)$row['nega'];
$result = mysqli_query($mysql, "select count(*) as zero from message where attitude=0");
$row = mysqli_fetch_assoc($result);
$y=(int)$row['zero'];
$result = mysqli_query($mysql, "select count(*) as posi from message where attitude=1");
$row = mysqli_fetch_assoc($result);
$z=(int)$row['posi'];
mysqli_free_result($result);
mysqli_close($mysql);
$total=$x+$y+$z; //生成统计图部分
$height=200;
$width=200;$y1=$height-intval($x/$total*$height,10);
$y2=$height-intval($y/$total*$height,10);
$y3=$height-intval($z/$total*$height,10); $im=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($im,255,255,255);
$black=imagecolorallocate($im,0,0,0);
$red=imagecolorallocate($im,255,0,0);
$blue=imagecolorallocate($im,0,0,255);
$green=imagecolorallocate($im,0,255,0);imagefill($im,0,0,$white);
imagerectangle($im,0,0,$width-1,$height-1,$black);
imagefilledrectangle($im,$width/10,$y1,3*$width/10,$height-2,$red);
imagefilledrectangle($im,4*$width/10,$y2,6*$width/10,$height-2,$blue);
imagefilledrectangle($im,7*$width/10,$y3,9*$width/10,$height-2,$green);imagestring($im,5,30,180,$x,$white);
imagestring($im,5,90,180,$y,$white);
imagestring($im,5,150,180,$z,$red);Header('Content-type:image/png');
imagepng($im);
imagedestroy($im);
?>
给$x, $y, $z分别整数赋值,可以显示图像
用echo $x; echo $y; echo $z;
可以看到$x, $y, $z的整数数值都已经存在
header小写
1、去掉 Conn.php 文件中最后的 ?>
2、确认两个文件均没有 BOM 头
3、确认 $x+$y+$z != 0
$result = mysqli_query($mysql, "select count(*) as nega from message where attitude=-1");
$row = mysqli_fetch_assoc($result);
$x=(int)$row['nega'];
$result = mysqli_query($mysql, "select count(*) as zero from message where attitude=0");
$row = mysqli_fetch_assoc($result);
$y=(int)$row['zero'];
$result = mysqli_query($mysql, "select count(*) as posi from message where attitude=1");
$row = mysqli_fetch_assoc($result);
$z=(int)$row['posi'];
mysqli_free_result($result);
mysqli_close($mysql); echo $x; echo "<br />";
echo $y; echo "<br />";
echo $z; echo "<br />";
?>显示的结果是:
0
22
5
$x=0; $y=22; $z=5;
$total=$x+$y+$z; //生成统计图部分
$height=200;
$width=200;$y1=$height-intval($x/$total*$height,10);
$y2=$height-intval($y/$total*$height,10);
$y3=$height-intval($z/$total*$height,10); $im=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($im,255,255,255);
$black=imagecolorallocate($im,0,0,0);
$red=imagecolorallocate($im,255,0,0);
$blue=imagecolorallocate($im,0,0,255);
$green=imagecolorallocate($im,0,255,0);imagefill($im,0,0,$white);
imagerectangle($im,0,0,$width-1,$height-1,$black);
imagefilledrectangle($im,$width/10,$y1,3*$width/10,$height-2,$red);
imagefilledrectangle($im,4*$width/10,$y2,6*$width/10,$height-2,$blue);
imagefilledrectangle($im,7*$width/10,$y3,9*$width/10,$height-2,$green);imagestring($im,5,30,180,$x,$white);
imagestring($im,5,90,180,$y,$white);
imagestring($im,5,150,180,$z,$red);Header('Content-type:image/png');
imagepng($im);
imagedestroy($im);
?>上面代码可生成柱状图
//Header('Content-type:image/png');
注释掉header后,在用wamp模拟,问题好像解决了,可以从数据库中读数据和显示图像了
但注释掉header后, 传到网站就出现一堆乱码,仍然不行
print_r(unpack('H*', $s));得到
Array ( [1] => efbbbf89504e470d0a1a0a0000000d49484452000000c8000000c80802....这个 efbbbf 不就是 BOM 头吗?你怎么说没有呢?
是我的过错,我以为上传的conn文件已经去掉了BOM,结果它没去掉,现在问题已经解决