ai didi

php - 如何从mysql中选择一篇文章及其评论?

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:39 24 4
gpt4 key购买 nike

我用两个单独的查询从 mysql 数据库中读取了一篇文章及其评论

$result = mysql_query("SELECT * FROM articles WHERE article_id='$id'");
$row = mysql_fetch_array($result);
$title=$row['title'];
........

$result = mysql_query("SELECT * FROM comments WHERE article_id='$id'");
while($row = mysql_fetch_array($result)) {
$comment_title=$row['title'];
.........
}

从数据库中读取这组数据的最佳方式是什么?或者是否可以通过一次查询或一次交易来捕获数据?

注意:我的问题是第一次查询文章只针对一行;但第二个需要一个循环来处理(并以 html 格式显示)多个评论。

最佳答案

$result = mysql_query("SELECT articles.title as article_title, comments.title as comment_title FROM articles LEFT JOIN comments ON articles.article_id = comments.article_id WHERE articles.article_id = '$id'");
$row = mysql_fetch_array($result);
$article_title=$row['article_title'];
$comment_title=$row['comment_title '];

关于php - 如何从mysql中选择一篇文章及其评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8119904/

24 4 0
文章推荐: android - AndEngine如何从SD卡加载spritesheet
文章推荐: c# - Entity Framework 处理并发
文章推荐: android - 网络调用在 android 中抛出 IO 异常和连接被拒绝在 android 设备中不工作
文章推荐: php - 如何在 MySQL/PHP 中按多个字段进行过滤
太空宇宙
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com