语法错误(语法明显好)

时间:2011-02-23 01:02:29

标签: php syntax-error

// 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>

6 个答案:

答案 0 :(得分:3)

echo "<p>$choice_count individuals have taken part in this survey: </p>n\";

应该是

echo "<p>$choice_count individuals have taken part in this survey: </p>\n";

你只是放错了逃避的正斜杠,转义了引号而不是换行符。当发生这种情况时,字符串在技术上不会结束,并且您遇到问题。虽然错误在第63行,但PHP读取63是预期的,64是问题。如果T_ENCAPSED错误,如果在该行上没有看到任何内容,请务必检查周围的行。

答案 1 :(得分:1)

上面一行中的echo语句转义了结尾“。删除\”,或添加一个尾随的“。或者你真正想要的是\ n”而不是n \“。你错过了其他一些”同样。

答案 2 :(得分:1)

这一行没问题,错误发生在您意外转义报价的前一行。

echo "<p>$choice_count individuals have taken part in this survey: </p>n\";

请注意,n\应为\n

答案 3 :(得分:1)

您的错误位于前一行:

echo "<p>$choice_count individuals have taken part in this survey: </p>n\";

你转义了最后的双引号,所以字符串继续到下一行。

答案 4 :(得分:1)

看起来你有$ mysqldname而不是:$ mysqldbname

在名称中缺少B可能会导致错误的关联。?

答案 5 :(得分:0)

可能是PHP编辑器的问题,请确保使用专业的PHP编辑器进行编码。

如果您使用常规文本编辑器进行PHP编码,那么它可以添加隐藏行和不需要的字符,这可能会在解析PHP代码时导致问题。

这就是你如何错过 - 判断可见的第64行,而包括隐藏的线可能会落在另一行上。

这只是小心!