语法错误(语法显然不错)
// the following array description (line 64 of the code) is, to my eye, complete and accurate:
$choicetext = array("", "C/C++", "Java", "Perl", "PHP", "VB/VBA/VBScript", "Andere");
// but it returns this error message:
解析错误:语法错误,意外 T_CONSTANT_ENCAPSED_STRING,期待 ',' 或者 ';'在 /Library/WebServer/Documents/results.php 第 64 行
我看来,好像是逗号和结尾 ';'都在正确的地方。我在网上搜索了 T_CONSTANT_ENCAPSED_STRING,但我发现讨论中解决的异常与此不同。如果有人能纠正我,我将不胜感激。
这是整个网页——使用 PHP 和 MySQL 的练习:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Survey Results</title>
</head>
<body>
<h2>Survey Results</h2>
<?php
$mysqlhost="localhost";
$mysquluser="zen";
$mysqlpasswd="••••••••••";
$mysqldname="test_vote";
// create a connection to the database
$link =
@mysql_connect($mysqlhost, $mysqluser, $mysqlpasswd);
if($link==FALSE) {
echo "<p><b>Unfortunately, a connection to the database cannot be made and the results cannot be displayed at this time. Please try again later.</b></p>
</body></html>\n";
exit();
}
mysql_select_db($mysqldbname);
// if questionarre data are available;
// evalutate + store
function array_item($ar, $key) {
if(array_key_exists($key, $ar)) return($ar[$key]);
return(''); }
$submitbutton = array_item($_POST, 'submitbutton');
$vote - array_item($_POST, 'vote');
if($submitbutton=="OK") {
if($vote>=1 && $vote<=6) {
mysql_query(
"INSERT INTO votelanguage (choice) VALUES ($vote)");
}
else {
echo "<p>Not a valid selection. Please vote again. Back to <a href=\"vote.html\">questionnaire</a>.</p>
</body></html>\n";
exit();
}
}
// display results
echo "<p><b>What is your favorite programming language for developing MySQL applications?</b></p>\n";
// number of votes cast
$result =
mysql_query("SELECT COUNT(choice) FROM votelanguage");
$choice_count = mysql_results($result, 0, 0);
// percentages for individual voting categories
if($choice_count == 0) {
echo "<p>$choice_count No one has voted yet.</p>\n";
}
else {
echo "<p>$choice_count individuals have taken part in this survey: </p>n\";
$choicetext = array("", "C/C++", "Java", "Perl", "PHP", "VB/VBA/VBScript", "Andere");
print("<p><table>\n;
for($i=1; $i<=6; $i++) {
$result = mysql_query(
"SELECT COUNT(choice) FROM votelanguage".
"WHERE choice - $i");
$choice[$i] = mysql_result($result, 0, 0);
$percent - round($choice[$i]/$choice_count*10000)/100;
print("<tr><td>$choicetext[$i]:</td>");
print("<td>$percent %</td></tr>\n");
}
print("</table></p>\n");
}
?>
</body>
</html>
</body>
</html>
// the following array description (line 64 of the code) is, to my eye, complete and accurate:
$choicetext = array("", "C/C++", "Java", "Perl", "PHP", "VB/VBA/VBScript", "Andere");
// but it returns this error message:
Parse error: syntax error, unexpected
T_CONSTANT_ENCAPSED_STRING, expecting
',' or ';' in
/Library/WebServer/Documents/results.php
on line 64
It looks to me as if the commas and the ending ';' are in the right places. I have searched the 'Net for T_CONSTANT_ENCAPSED_STRING but anomalies addressed in the discussions I found are not the same as this one. If anyone can set me straight I will be grateful.
Here is the entire web page—an exercise in using PHP and MySQL:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Survey Results</title>
</head>
<body>
<h2>Survey Results</h2>
<?php
$mysqlhost="localhost";
$mysquluser="zen";
$mysqlpasswd="••••••••••";
$mysqldname="test_vote";
// create a connection to the database
$link =
@mysql_connect($mysqlhost, $mysqluser, $mysqlpasswd);
if($link==FALSE) {
echo "<p><b>Unfortunately, a connection to the database cannot be made and the results cannot be displayed at this time. Please try again later.</b></p>
</body></html>\n";
exit();
}
mysql_select_db($mysqldbname);
// if questionarre data are available;
// evalutate + store
function array_item($ar, $key) {
if(array_key_exists($key, $ar)) return($ar[$key]);
return(''); }
$submitbutton = array_item($_POST, 'submitbutton');
$vote - array_item($_POST, 'vote');
if($submitbutton=="OK") {
if($vote>=1 && $vote<=6) {
mysql_query(
"INSERT INTO votelanguage (choice) VALUES ($vote)");
}
else {
echo "<p>Not a valid selection. Please vote again. Back to <a href=\"vote.html\">questionnaire</a>.</p>
</body></html>\n";
exit();
}
}
// display results
echo "<p><b>What is your favorite programming language for developing MySQL applications?</b></p>\n";
// number of votes cast
$result =
mysql_query("SELECT COUNT(choice) FROM votelanguage");
$choice_count = mysql_results($result, 0, 0);
// percentages for individual voting categories
if($choice_count == 0) {
echo "<p>$choice_count No one has voted yet.</p>\n";
}
else {
echo "<p>$choice_count individuals have taken part in this survey: </p>n\";
$choicetext = array("", "C/C++", "Java", "Perl", "PHP", "VB/VBA/VBScript", "Andere");
print("<p><table>\n;
for($i=1; $i<=6; $i++) {
$result = mysql_query(
"SELECT COUNT(choice) FROM votelanguage".
"WHERE choice - $i");
$choice[$i] = mysql_result($result, 0, 0);
$percent - round($choice[$i]/$choice_count*10000)/100;
print("<tr><td>$choicetext[$i]:</td>");
print("<td>$percent %</td></tr>\n");
}
print("</table></p>\n");
}
?>
</body>
</html>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
应该是
您刚刚放错了转义正斜杠,转义了引号而不是换行符。当发生这种情况时,字符串在技术上并没有结束,并且您会遇到问题。尽管错误出现在第 63 行,但 PHP 读取到 63 是预期的,而 64 是问题所在。对于 T_ENCAPSED 错误,如果您在该行上没有看到任何内容,请务必检查周围的行。
should be
You've just misplaced your escaping forward slash, escaping the quote instead of the newline character. When this happens, the string doesn't technically end, and you run into problems. Although the error is on line 63, PHP reads that 63 is intended and that 64 is the problem. With T_ENCAPSED errors, always check the surrounding lines if you don't see anything on that line.
上面一行中的 echo 语句转义了结尾的“。删除 \”,或添加尾随的“。或者您真正想要的是 \n”而不是 n\”。您还缺少其他一些“。
The echo statement in the line above escapes the closing ". Remove the \", or add a trailing ". Or maybe what you really want is \n" rather than n\". You are missing some other "'s as well.
这行没问题,错误发生在上一行,您不小心转义了引号。
请注意,
n\
应为\n
。This line is fine, the error is on the previous line where you accidentally escaped a quote.
Note the
n\
should be\n
.您的错误位于前一行:
您转义了最后的双引号,因此字符串继续到下一行。
Your error is on the prior line:
You escaped the final double quote so the string is continuing to the next line.
看起来你有 $mysqldname 而不是: $mysqldbname
名称中缺少 B,可能会导致错误的关联。?
Looks like you have $mysqldname instead of : $mysqldbname
Missing a B in the name, could lead to wrong associations.?
可能是PHP编辑器的问题,请确保使用专业的PHP编辑器进行编码。
如果您使用通用文本编辑器进行PHP编码,那么它可能会添加隐藏行和不需要的字符,这可能会导致解析 PHP 代码时出现问题。
这就是你可能会误判可见线 64 的原因,而包含隐藏线则可能会落在另一条线上。
这只是警告!
It could be issue of PHP Editor, make sure to use professional PHP editor for coding.
If you are using General Text Editor for PHP coding, then it can add up hidden lines and unwanted characters, which can cause issue while parsing PHP code.
Thats how you may miss-judge visible Line 64, whereas including hidden lines it may fall on other one.
This is just caution !