【发布时间】:2015-08-09 02:41:04
【问题描述】:
我正在尝试将两个表连接在一起。在 posts 表中,我有两列 post_id 和 post_message。
在另一个表名post_attach中有row_id、postid(同post_id)和file_name(同ms.jpg)。
一个帖子可能有多个附件。但是我不知道要查询什么来显示一个带有多个附件的帖子。这是我的查询...
$s = " SELECT posts.post_id,post_message, post_attach.file_name from posts join post_attach on post_id=row_id;
<?php
$conn = mysqli_connect('localhost', 'root', 'root', 'blog') or die();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h3>Allo post with their attahcment</h3>
<?php
$s = " SELECT posts.post_id,post_message, post_attach.file_name from posts join post_attach on post_id=row_id;
";
$q = mysqli_query($conn,$s);
while ($row = mysqli_fetch_array($q)) {?>
<p>Post No: <?php echo $row['post_id'] ?></p>
<p>Post text: <?php echo $row['post_message'] ?></p>
<p>Post attach: <?php echo $row['file_name'] ?></p>
<?php } ?>
</body>
</html>
我想要类似的东西:
post no: 1, post text: hello php, 附件: ms.jpg, ms.jpeetc.
最后一句话是每个帖子都会显示它们的相关附件。
【问题讨论】:
-
您的查询输出了什么?检查了吗?