意想不到的'}使用 SQL 和 PHP 绘制图表时

发布于 2024-10-17 03:37:50 字数 1648 浏览 1 评论 0原文

我正在使用 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

I'm coding a graph from SQL using PHP. I've got this code and it doesn't seem to execute. I thought the code was right but apprently it's not.

the code is:

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

When I use this I get the error:

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

Thanks guys! This code doesn't seem to work here is the error log. I'm learning PHP and SQL as well so any help is appreciated. - http://pastebin.com/Zw18Afne

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2024-10-24 03:37:50
 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. ?>

好的,让我们看一下错误:

解析错误:语法错误,/home/user/public_html/graph.php 第 27 行出现意外的“}”

好的,该错误基本上是说,PHP 发现了一个它没有预料到的 } ,原因是 php 会跳过代码中的空格、换行符和任何不可见的字符,然后解释代码。

所以 PHP 看到你这样的代码

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

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

在 PHP 中每行命令都应该用 ; 结束,以便 PHP 知道该段代码的结尾,并且第二个代码不是第一个代码的一部分。

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

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

 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. ?>

ok so let's take a look at the error then:

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

Ok the error is basically saying, that PHP Found a } of witch it didn't expect, the reason for this is that php skips spaces, new lines and any invisible character from the code and then interprets the code.

So PHP Sees you code like this

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

But as you can see, i have placed an arrow where php reaches and says, what.. there should not be a } here :/

Within PHP Each line of command should be closed with a ; so that PHP knows that the end of that bit of code, and the second is not part of the first.

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

placing the colon here tells php to move on to the next command.

总以为 2024-10-24 03:37:50

你后面少了一个分号

$xval+=($barwidth+10)

You're missing a semicolon after

$xval+=($barwidth+10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文