【问题标题】:Why am I seeing multiply rows from database为什么我从数据库中看到多行
【发布时间】:2014-06-12 23:19:50
【问题描述】:

这是我的代码,它也与数据库连接,但看到图像,为什么它显示 2 次相同的 ID 和其他行?

这是它所显示的屏幕截图:/

<?php
$username = $_SESSION['usr'];
        // get results from database
        $result = mysql_query("SELECT * FROM playlist, tz_members WHERE Byuser='$username'") 
                or die(mysql_error());  


        echo "<table id='table_example'><thead>";
        echo "<tr><th width='1%'>ID</th><th width='10%'>Kengtar(<i><span style='color: blue'>i</span></i>/<i>ja</i>)</th><th width='5%'>Kenga</th><th width='7%'>Albumi</th><th width='2%'>Data</th></tr>";
        echo "</thead><tbody>";

        while($row = mysql_fetch_array( $result )) {

        echo "<tr>";
        echo '<th>#' . $row['ID'] . '</th>';
        echo '<td>' . $row['Kengtar'] . '</td>';
        echo '<td>' . $row['Kenga'] . '</td>';
        echo '<td>' . $row['Albumi'] . '</td>';
        echo '<td>' . $row['Data'] . '</td>';
        echo "</tr>";
    }
        echo "</table>";
?>

谢谢你,如果有人试图帮助我,我是 php codind 的新手:S

【问题讨论】:

  • 我们可以获取您的数据库的屏幕截图吗?我猜您在 sql 语句中查询的两个表中都有这些行。
  • 当然可以,但我可以在这里解释播放列表有(ID、Kengtar、Kenga、Linku、Data、Byuser),而 tz_members 只有 memberID、用户名密码
  • 谢谢你解决了,我只需要从选择中删除 tz_members 我很高兴是的哈哈哈非常感谢你

标签: php database error-handling rows


【解决方案1】:

您的查询对 2 个表进行了笛卡尔积。相反,您需要使用以下条件连接表:

select * 
from playlist a, 
     tz_members b
where Byuser='$username' 
      and a.id = b.id

当然,您需要将a.id = b.id更改为实际关系。

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2019-05-02
    • 2012-05-11
    • 2019-12-12
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多