为什么这个PHP代码会产生解析错误

alg*_*der -2 php

对于我的迷你论坛,我有一个"主题"表,其中包含字段id(int),title(txt)和post(txt),我试图让用户根据他们通过topicPage.php点击的主题来查看不同的页面:

  <?
//send a query to retrieve all titles from the topics database
$query = "SELECT * FROM topics" ;
$result = mysql_query($query) or die ("something went wrong with this query");

    ?>

<span class="subtext">  <?
while($row = mysql_fetch_array($result))
{
echo "<a href = 'topicPage.php?tid=$row['id']'>" .$row['title']. " </a>"." <br>" ." <br>";

}
?>
Run Code Online (Sandbox Code Playgroud)

我在echo函数的行上得到一个解析错误.

这是确切的错误:

解析错误:语法错误,意外T_ENCAPSED_AND_WHITESPACE,在第85行的/index.php中期待T_STRING或T_VARIABLE或T_NUM_STRING

Pas*_*TIN 5

以下部分:

echo "<a href = 'topicPage.php?tid=$row['id']'>";
Run Code Online (Sandbox Code Playgroud)

过于复杂.


您必须使用Complex(卷曲)语法帮助PHP找出变量是什么:

echo "<a href = 'topicPage.php?tid={$row['id']}'>";
Run Code Online (Sandbox Code Playgroud)

  • 它不是太复杂,只是错误的语法.写"echo"<a href ='topicPage.php?tid=$row[id]'>";`会工作;) (2认同)