开发者

Why does this PHP code give a Parse Error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

for my mini forum I have a "topics" table with fields id(int), title(txt) and post(txt) and I am trying to get the user to see different pages depending on the topics they click on via 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>" ." &l开发者_JAVA技巧t;br>";

}
?>

I get a parse error on the line with the echo function.

Here's the exact error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /index.php on line 85


The following part :

echo "<a href = 'topicPage.php?tid=$row['id']'>";

is too complex.


You must help PHP find out what is the variable, using the Complex (curly) syntax :

echo "<a href = 'topicPage.php?tid={$row['id']}'>";


Using single quotes these kind of issues seldom appear:

echo '<a href="topicPage.php?tid='.$row['id'].'">'.$row['title'].'</a><br /><br />';

Also, you don't need to concatenate the <br /> tags like that, you can supply them in one big hunk of string.


echo "<a href = 'topicPage.php?tid=" . $row['id'] . "'>" .$row['title']. " </a>"." <br>" ." <br>";

try to keep same style ...


Treat $row['id'] the same way as $row['title'].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜