将SQL和PHP用于图形时出现意外的'}'

时间:2011-02-14 18:10:56

标签: php sql mysql graph

我正在使用PHP编写SQL图形。我有这个代码,它似乎没有执行。我认为代码是正确的,但显然它不是。

代码是:

<?php
header ("Content-type: image/png");
$im = imagecreatefrompng ("graphtemp.png");
$red = imagecolorallocate ($im, 255, 0, 0);
$black = imagecolorallocate ($im, 0, 0, 0);
mysql_connect("localhost", "user", "password");
mysql_query("USE database");
$optionsquery = mysql_query("SELECT * FROM brands");
$numoptions = mysql_num_rows($optionsquery);
$pollquery = mysql_query("SELECT * FROM categories");
$numvotes = mysql_num_rows($pollquery);
$xval = 30;
$barwidth = floor(300/$numoptions);
for ($i=0;$i<=($numoptions-1);$i++) 
{
    $voteoption = mysql_result($optionsquery,$i,'name');
    $votevalue = mysql_result($optionsquery,$i,'value');
    $currentnumquery = mysql_query("SELECT * FROM categories WHERE vote='$votevalue'");
    $currentnum = mysql_num_rows($currentnumquery);
    $per = floor(($currentnum/$numvotes)*184);
    $rper = floor(($currentnum/$numvotes)*100);
    imagefilledrectangle ($im, $xval, (200-$per), ($xval+$barwidth), 200, $red);
    imagerectangle ($im, $xval, (200-$per), ($xval+$barwidth), 200, $black);
    imagestring ($im, 1, ($xval+($barwidth/2)), 205, $voteoption, $black);
    imagestring ($im, 2, ($xval+($barwidth/2)), ((200-$per)-15), "$rper%", $bla);
    $xval+=($barwidth+10)
}
imagepng($im);
?>

当我使用它时,我收到错误:

 Parse error: syntax error, unexpected '}' in /home/user/public_html/graph.php on line 27

谢谢你们!这段代码似乎不起作用是错误日志。我正在学习PHP和SQL,所以任何帮助都表示赞赏。 - http://pastebin.com/Zw18Afne

2 个答案:

答案 0 :(得分:2)

 1. <?php
 2. header ("Content-type: image/png");
 3. $im = imagecreatefrompng ("graphtemp.png");
 4. $red = imagecolorallocate ($im, 255, 0, 0);
 5. $black = imagecolorallocate ($im, 0, 0, 0);
 6. mysql_connect("localhost", "user", "password");
 /*
     SNIP
 */
 24.    imagestring ($im, 1, ($xval+($barwidth/2)), 205, $voteoption, $black);
 25.    imagestring ($im, 2, ($xval+($barwidth/2)), ((200-$per)-15), "$rper%", $bla);
 26.    $xval+=($barwidth+10)
 27. }
 28. imagepng($im);
 29. ?>

好的,那么让我们来看看错误:

  

解析错误:第27行/home/user/public_html/graph.php中的语法错误,意外'}'

好的错误基本上是说PHP发现了}的女巫它没想到,其原因是php从代码中跳过空格,新行和任何不可见的字符然后解释代码。

所以PHP看到你这样的代码

$xval+=($barwidth+10)}imagepng($im);
//                   ^

但正如你所看到的,我已经放置了一个箭头,其中php到达并说,什么......这里不应该有}:/

在PHP中,每一行命令都应该用;关闭,这样PHP才能知道该位代码的结尾,而第二行不是第一行代码的一部分。

26.    $xval+=($barwidth+10); // ; here
27. }
28. imagepng($im);
29. ?>

将冒号放在这里告诉php继续下一个命令。

答案 1 :(得分:1)

之后你错过了一个分号
$xval+=($barwidth+10)