2012-04-15 125 views
0

我使用PHP创建了一个简单的网站,用户可以提交博客,其他用户(已登录的用户)可以发表评论。我在每个博客下面创建了一个名为“评论”的链接,点击后链接将显示/隐藏与特定博客相关的所有评论(同样,如果用户已登录,它将显示一个表单域,用于提交新评论) 。所以基本上每个博客都会有多个评论。我为此做了两个不同的代码,但它们都有相同的问题,每个注释出现两次(一切正常)。有谁能指出原因吗?执行两次的循环结果

mysql_select_db ("ooze"); 
$result = mysql_query ("select * from blog") or die(mysql_error()); 
$i = 1; 
while($row = mysql_fetch_array($result)) 
{ 
    echo "<h1>$row[title]</h1>"; 
    echo "<p class ='second'>$row[blog_content]</p> "; 
    echo "<p class='meta'>Posted by .... &nbsp;&bull;&nbsp; $row[date] &nbsp;&bull;&nbsp; <a href='#' onclick=\"toggle_visibility('something$i'); return false\">Comments</a><div id='something$i' style='display: none;'>";  
    $i++; 
    $a = $row["ID"]; 
    $result2 = mysql_query ("select * from blog, blogcomment where $a=blogID") or die(mysql_error()); 
    while($sub = mysql_fetch_array($result2)) 
    { 
    echo "<p class='third' >$sub[commentdate] &nbsp;&bull;&nbsp; $sub[username]</p><p>said:</p> <p>$sub[comment]</p>"; 
    } 
if (isset ($_SESSION["gatekeeper"])) 
{ 
    echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>'; 
} 
else 
{ 
    echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>'; 
} 
echo "</div>"; 
} 
mysql_close($conn); 

//第二内环的版本://

if (isset ($_SESSION["gatekeeper"])) 
{ 
    while($sub = mysql_fetch_array($result2)) 
    { 
    echo "<p class='third' >$sub[commentdate] &nbsp;&bull;&nbsp; $sub[username] said:</p> <p>$sub[comment]</p>"; 
    } 
echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>'; 
} 
else 
{ 
    while($sub = mysql_fetch_array($result2)) 
    { 
    echo "<p class='third' >$sub[commentdate] &nbsp;&bull;&nbsp; $sub[username] said:</p> <p>$sub[comment]</p>"; 
    } 
echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>'; 
} 
echo "</div>"; 
} 
mysql_close($conn); 
+0

请检查您的第二个查询:'select * from blog,blogcomment where $ a = blogID'您要加入两个表格但未指定导致“交叉连接”的关系(http://en.wikipedia .org/wiki/Join_(SQL)#Cross_join) – Yaniro 2012-04-15 11:23:48

回答

0

你的问题就出在从第一例此查询。

$result2 = mysql_query ("select * from blog, blogcomment where $a=blogID") 

您已查询博客表,因此无需再次查询。只需将其更改为

$result2 = mysql_query ("select * from blogcomment where $a=blogID") 

应解决问题。

但是有很多事情需要考虑。

  • 你为什么要重新发明轮子?那里有很多好的博客应用程序。你最好使用其中之一。
  • 不建议再使用mysql_系列函数。走开并学习mysqli_或更好的PDO
  • 您应该了解separation of concerns。至少您应该确保您的数据访问/业务逻辑与显示逻辑分开。 MVC在PHP中很常见。
  • 您还应该了解JOIN。即使在这个简单的内联脚本中,您也可以在循环内进行查询,但效率不高。你可以将你的查询合并成一个(就像你用内部查询尝试过的那样)。区别在于一个查询应该在主循环之外。
+0

我正在为uni项目做这个工作,所以我只是在练习我演讲过的东西。感谢您的回答,它的工作 – ozzysmith 2012-04-15 11:32:10

+0

@ozzysmith足够公平,这回答我的第一点。其他都是相关的。如果你的讲师展示了你上面发布的代码,我希望你不会为这门课程付费。我会索要你的钱。 – liquorvicar 2012-04-15 11:37:55