列出数据库中ID正确的广告';s来自某个用户


Listing ads from database with correct ID's from a certain user

所以我有以下SQLQuery:

$user = new User();
$nome = escape($user->data()->username);
$ligacao = mysqli_connect("localhost","root","","publicidade") or die("Erro MySQL");
$sql = "SELECT * FROM ads, users WHERE users.username = '$nome' AND users.id = ads.cod_user";
$resultado = mysqli_query($ligacao, $sql);

接下来是要列出的当前代码:

while(($registo = mysqli_fetch_assoc($resultado)) != null)
    {
    ?>
        <tr>
            <td><?php echo $registo["id"]?></td>
            <td><?php echo $registo["content"]?></td>
            <td><?php echo $registo["image"]?></td>
            <td><?php echo $registo["total_views"]?></td>
            <td><?php echo $registo["contract_views"]?></td>
            <td><?php echo $registo["active"]?></td>
            <td><?php echo $registo["cod_type"]?></td>
            <td><?php echo $registo["cod_organization"]?></td>
            <td><a href="editAd.php?id=<?php echo $registo["id"]; ?>">ALTERAR</a></td>
            <td><a href="removeAd.php?id=<?php echo $registo["id"]; ?>">REMOVER</a></td>
        </tr>
    <?php

我想要一件事,那就是列出该用户制作的所有广告(它有效),但要有正确的ID,在我的情况下,它应该是类似于"1"3"6《》"9》的内容。问题是,它列出了它们,但用户ID为"1"。

<td><?php echo $registo["id"]?></td>

我应该在那里换点什么吗?

您可以将*拆分为引用表名的列的确切列表,并为ads.id指定一个特殊名称:SELECT ... , ads.id as AdID FROM ...

或者,您可以更改其中一个表中id列的名称。这会迫使你在使用它的任何地方都要更改它。因此,我会选择第一种方案。