Php 为什么条形图代码没有';不行?

Php 为什么条形图代码没有';不行?,php,Php,数据库是正确的: DATABASE `poll`; TABLE `results` CREATE TABLE `results` ( book_type VARCHAR(50), num_votes INT ); INSERT INTO `results` values ('Classic', 15), ('Fantasy', 7), ('Humor', 32), ('Mystery', 12), ('Poetry', 25);

数据库是正确的:

DATABASE `poll`; TABLE `results`  CREATE TABLE `results` ( 
   book_type VARCHAR(50), 
   num_votes INT
);

INSERT INTO `results` values  
    ('Classic', 15), 
    ('Fantasy', 7), 
    ('Humor', 32),
    ('Mystery', 12),
    ('Poetry', 25);
代码

<?php
    $dbhandle = mysql_connect("localhost","root","123") or die("unable to connect to mysql");

    $selected = mysql_select_db("poll",$dbhandle);

    $result = mysql_query("SELECT * FROM results");

    $num_poller = mysql_num_rows($result);

    $total_votes = 0;
    while($row = mysql_fetch_array($result)){
        $total_votes += $row{'num_votes'};

    }

    mysql_data_seek($result,0);
    mysql_close($dbhandle);

    putenv('GDFONTPATH=C:\WINDOWS\Fonts');
    $font = 'arial';
    $y = 50;
    $width =700;
    $bar_height =20;
    $height = $num_poller * $bar_height *1.5 + 70;
    $bar_unit = ($width - 400)/100;

    $image = imagecreate($width,$height);

    $white = imagecolorallocate($image,255,255,255);
    $black = imagecolorallocate($image,0,0,0);
    $red = imagecolorallocate($image,255,0,0);
    $blue = imagecolorallocate($image,0,0,255);

    imagefill($image,$width,$height,$white);

    imagerectangle($image,0,0,$width-1,$height-1,$black);

    imagettftext($image,16,0,$width/3+50,$y-20,$black,$font,'poll results');

    while($row = mysql_fetch_object($result)){
        if($total_votes > 0){
            $percent = intval(round(($row->num_votes/$total_votes)*100));

        }else{
            $percent =0;

    }

    imagettftext($image,12,0,10, $y+($bar_height/2), $black, $font, $row->book_type);
    //Output percentage for a particular value
    imagettftext($image, 12, 0, 170, $y + ($bar_height/2),$red,$font,$percent.'%');

    $bar_length = $percent * $bar_unit;

    //Draw a shape that corresponds to 100%
    imagerectangle($image, $bar_length+221, $y-2, (220+(100*$bar_unit)), $y+$bar_height, $black);
    //Output a bar for a particular value
    imagefilledrectangle($image,220,$y-2,220+$bar_length, $y+$bar_height, $blue);
    //Output the number of votes
    imagettftext($image, 12, 0, 250+100*$bar_unit, $y+($bar_height/2), $black, $font, $row->num_votes.' votes cast.');


    $y = $y + ($bar_height * 1.5);

    }    

    header("Content-Type: image/jpeg");    

    imagejpeg($image);    

    imagedestroy($image);

要了解您似乎遇到的实际错误,您需要像@Wesley在评论中建议的那样打开错误报告<代码>错误\重新打印(-1)
然后查看
php.错误日志
或删除
标题(“内容类型:image/jpeg”)
因此,如果您已打开
display\u errors
,则会在浏览器中显示错误

通常会出现“格式错误”错误,因为响应如下所示:

Warning: Some error.....
ZWW$%$%BINARY-IMAGE-STUFF

您的浏览器无法从该输出中获取正确的jpeg头。

应首先打开。我添加了错误报告(E\u ALL);先把代码修改一下。仍然没有任何错误提示。检查代码并退出(var_dump($some_variables)),确保看到应该看到的内容。Wesley Murch,当我退出(var_dump($image))时。它输出(gd)类型的资源(5)。是这样吗?谢谢,听起来不错。我只是想给出一些基本的调试指针,但我不知道你的问题的答案。