2011-02-23 62 views
1
// 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語法錯誤(顯然具有良好的語法)

在我看來好像逗號和結束「;」是在正確的地方。我已經在'Net for 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> 

回答

1

以上線路回聲聲明逃脫關閉,或添加尾隨」或者。也許你真正想要的是\ n「而不是n \」。你也錯過了其他一些「。

1

這條線很好,錯誤是在你不小心逃過報價的前一行。

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

注意n\應該\n

+0

謝謝!全部......你們搖滾。我打算清理其餘的代碼並嘗試一下。 – zen 2011-02-23 01:10:42

1

你的錯誤是在之前行:

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

你這樣的字符串繼續下一行躲過了最後一個雙引號。

+0

蕩,這就像一場比賽在這裏,我失去了:-)歡迎 – BMitch 2011-02-23 01:06:06

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

看起來你有$ mysqldname代替:$ mysqldbname

在名稱中缺少一個B,可能會導致錯誤的聯想?

0

這可能是PHP編輯器的問題,請務必使用專業的PHP編輯器進行編碼。

如果您使用通用文本編輯器進行PHP編碼,那麼它可能會添加隱藏行和不需要的字符,這可能會在解析PHP代碼時導致問題。

這就是你可能會錯過判斷可見線64,而包括隱藏線可能落在另一個。

這只是謹慎!

+0

計算器來。 Stackoverflow不是論壇,請保留它的結構。這不僅僅是一個答案,而是一個評論。當你有足夠的聲望,你可以發表評論。 – 2017-08-25 17:05:59