从MySQL反映PHP中的子记录

时间:2012-05-16 10:35:22

标签: php mysql collections

我正在尝试使用2个单独的表从我的数据库中获取记录。查询是

public function get_pages_by_campign_id($campaignID) {
    $campaignID = $this->real_escape_string($campaignID);
    return $this->query("SELECT pid, campaignid FROM pages,campaigns WHERE 'pages.campaignid = campaigns.id'");
}

这可以通过以下方式发挥作用:

$counter = 1;
            $userID = PageDB::getInstance()->get_user_id_by_name($_SESSION['user']);
            $result = PageDB::getInstance()->get_pages_by_campaign_id($campaignID);
            $i=0;
                while ($row = mysqli_fetch_array($result)):
                 $style = "";
                    if($i%2==0)
    {
        $style = ' style="background-color: #EFEFEF"';
    }
    echo "<tr".$style.">";

                echo "<td style='width:140px;'> Page "  . $counter .  "</td>";
                echo "<td>&nbsp;</td>";
                echo "<td></td>";


                //The loop is left open
                ?>
                <td>
                <form style="display:none;"></form>
                    <form name="editPage" action="editPage.php" method="GET">
                        <input type="hidden" name="pageID" value="<?php echo $pageID = $row['pid']; ?>"/>
                        <input type="submit" name="editPage" value="Edit"/>
                    </form>
                </td>
                <td>
                    <form name="deletePage" action="deletePage.php" method="POST">
                        <input type="hidden" name="pageID" value="<?php echo $pageID = $row['pid']?>"/>
                        <input type="submit" name="deletePage" value="Delete" onclick = "javascript: return confirm('Delete Page <?php echo $counter ?> ?');"/>
                    </form>
                </td>
                <?php
                $pageID = $row['pid'];  
                $counter++;
                $i++;

                echo "</tr>\n";
            endwhile;
            mysqli_free_result($result);
            ?>

这应列出所有“页面”记录,但它没有返回任何内容。

我已在表格“广告系列”中设置了“id”列,以使用外键与表格“pages”中的“campaignid”对应。

我用这个来修理它:

public function get_pages_by_campaign_id($campaignID) {
    $campaignID = $this->real_escape_string($campaignID);
    return $this->query("SELECT pid, campaignid FROM pages WHERE campaignid = 'campaigns.id'");
}

1 个答案:

答案 0 :(得分:0)

"SELECT pid, campaignid FROM pages,campaigns WHERE pages.campaignid = campaigns.id"