如何在每个MySQL字段结果之间添加标记?

时间:2012-12-26 21:38:37

标签: php html mysql

我正在为我的网站在PHP中创建一个评论框。这是代码:

$con = mysql_connect($hostname,$username,$password); // Connect to MySQL database
if (!$con)
  {
  die("Could not connect: " . mysql_error()); 
  }
mysql_select_db($dbname);
if(isset($_POST["submit"]))
{
    $comment=$_POST["comment"];
    $q="INSERT INTO comments_table (comments) VALUES ('$comment')"; // Could also be (\"comment\")
    mysql_query($q);
} 
$q="SELECT comments FROM comments_table";
$result=mysql_query($q);
while($row=mysql_fetch_array($result))
{
    // List the comments - how could I get some markup between each to make each have it's own area?
    echo $row['comments']."";
}
?>
<html>
<body>
<form method="post" action="/comments-test.php">
<textarea name="comment" rows=30 cols=10></textarea>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

我想知道的是我应该如何让每个评论都有它自己的框或标记。它不能是每个注释之间的相同HTML(例如,,</div><div class="comment">),否则在注释的末尾会有一些注释框没有结束标记和开头的杂散结束标记。我是SQL新手,但不是PHP。我该怎么做?

3 个答案:

答案 0 :(得分:1)

  // List the comments - how could I get some markup between each to make each have it's own area?
    echo '<div class="comment">'.$row['comments']."</div>";

那会有用吗?

答案 1 :(得分:0)

我不完全明白你在谈论什么,但你的意思是:

while($row=mysql_fetch_array($result))
{
    // List the comments - how could I get some markup between each to make each have it's own area?
    echo "<div class='comment'>".$row['comments']."</div>";
}

如果没有,那么我不确定你在说什么。

答案 2 :(得分:0)

只需将评论包装到正确的div

while($row=mysql_fetch_array($result))
{
    // List the comments - how could I get some markup between each to make each have it's own area?
    echo '<div class="comment">', $row['comments'], '</div>';
}