【发布时间】:2012-12-21 14:58:41
【问题描述】:
我已经搜索了我的问题,但似乎没有一个能够回答我的特殊需求。这是一个简单的表连接,但我尝试过的每件事都失败了。也许你可以帮忙。注意:所有行在 POSTS 中都有 post_id,但并非所有 post_id 在 CALENDAR 中都相关,即并非所有帖子都有日历条目。我在 PHP 中使用 MYSQL,而不是 MYSQLi。
POST_CAT- post_id, cat_id POSTS- post_id、标题、描述 CALENDAR- post_id, on_date, to_date
目前我通过多对多选择找到了相关的 post_ids,然后使用一系列 for 循环从单个表中输出。这行得通。但我需要将相关 post_id 的日历条目包含在一个数组中:
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("adamsmap_db") or die(mysql_error());
if (isset($_POST['activities'])) {
$post_id = mysql_query("SELECT post_id FROM post_cat WHERE cat_id = '1'");
while ($row = mysql_fetch_array($post_id, MYSQL_NUM)) {
for ($i=0;$i < count($row); $i++) {
$output = mysql_query("SELECT user_id, title, description FROM posts WHERE post_id='$row[$i]'");
while ($rowoutput = mysql_fetch_array($output, MYSQL_NUM)) {
for($i=0;$i < count($rowoutput);$i++) {
echo $rowoutput[$i]." ";
}
echo"<br /> <br />";
}}}}
【问题讨论】:
-
供未来用户看到您的问题。如果您知道这一点,那就太好了! Please, don't use
mysql_*functions in new code。它们不再维护,deprecation process 已开始使用。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.