Why does this PHP code give a Parse Error [closed]
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']
.
精彩评论