如何从mysql中选择一篇文章及其评论?
我从 mysql 数据库中阅读了一篇文章及其评论,并使用两个单独的查询作为
$result = mysql_query("SELECT * FROM articles WHERE article_id='$id'");
$row = mysql_fetch_array($result);
$title=$row['title'];
........
AND
$result = mysql_query("SELECT * FROM comments WHERE article_id='$id'");
while($row = mysql_fetch_array($result)) {
$comment_title=$row['title'];
.........
}
是从数据库读取这组数据的最佳方法吗?或者 是否可以通过一次查询或一笔交易来捕获数据?
注意:我的问题是文章的第一个查询仅针对一行;但第二个需要一个循环来处理(并以 html 形式显示)多个评论。
I read an article and its comments from mysql database with two separate queries as
$result = mysql_query("SELECT * FROM articles WHERE article_id='$id'");
$row = mysql_fetch_array($result);
$title=$row['title'];
........
AND
$result = mysql_query("SELECT * FROM comments WHERE article_id='$id'");
while($row = mysql_fetch_array($result)) {
$comment_title=$row['title'];
.........
}
Is the best way to read this set of data from database? OR
Is it possible to catch the data through one query or one transaction?
NOTE: My issue is that first query for article is only for one row; but the second one needs a loop to process (and display in html) several comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做 -
You can do it in this way -
当然,是的。
是的,这是可能的,但这样做没有意义。
你为什么问?
Sure, it is.
Yes, it's possible but there is no point in doing that.
Why do you ask?