PHP - 显示具有相同列值的数据库中的记录

时间:2017-01-08 00:48:59

标签: php mysql

我尝试使用相同的列值显示数据库中的某些记录。它正在显示,但问题是它被多次显示,具体取决于特定列值的行数。

示例:

ID | NAME | TOPIC | COMMENT
===x======x=======x========
1  | Jane |  ABC  |  hello
2  | Doe  |  ABC  |  hello
3  | Mary |  ABC  |  hello
4  | Pop  |  DEF  |  hello
5  | Tris |  DEF  |  hello

如果我试图通过TOPIC显示这些记录,它就像这样。

主题:ABC

ID | NAME | TOPIC | COMMENT
===x======x=======x========
1  | Jane |  ABC  |  hello
2  | Doe  |  ABC  |  hello
3  | Mary |  ABC  |  hello

ID | NAME | TOPIC | COMMENT
===x======x=======x========
1  | Jane |  ABC  |  hello
2  | Doe  |  ABC  |  hello
3  | Mary |  ABC  |  hello

ID | NAME | TOPIC | COMMENT
===x======x=======x========
1  | Jane |  ABC  |  hello
2  | Doe  |  ABC  |  hello
3  | Mary |  ABC  |  hello

它显示三次,因为该主题有3个条目。如果我选择一个主题,我想显示一次记录。

这是我的代码:

<?php
if(isset($_POST['sortBtn']))
{
    $sortEvent = $_POST['sort_event'];
    $sortQuery = "SELECT event_topic FROM tbl_comment";
    $sortResult = mysqli_query($dbcon, $sortQuery);

    while($row = mysqli_fetch_assoc($sortResult))
    {
        if($sortEvent == $row['event_topic'])
        {

            $query = "SELECT * FROM tbl_comment WHERE event_topic = '". $sortEvent . "'";
            $result = mysqli_query($dbcon, $query);

            echo "<table class='tbEvents' border='1'>";
                echo "<tr>";
                    echo "<th class='tHead'>#</th>";
                    echo "<th class='tHead'>User's Name</th>";
                    echo "<th class='tHead'>Event Topic</th>";
                    echo "<th class='tHead'>Comment</th>";
                    echo "<th class='tHead'>Date / Time</th>";
                echo "</tr>";
                if ($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {      
                        echo "<tr>";
                            echo "<td class='tData'>". $row["user_number"] ."</td>";
                            echo "<td class='tData'>". $row["user_nickname"] ."</td>";
                            echo "<td class='tData'>". $row["event_topic"] ."</td>";
                            echo "<td class='tData'>". $row["user_comment"] ."</td>";
                            echo "<td class='tData'>". $row["date_time"] ."</td>";

                        echo "</tr>";

                    }
                } else {
                    echo "0 results";
                }
            echo "</table>";
        }
    }
}
?>

我尝试过GROUP BY,ORDER BY,DISTINCT,但没有任何作用。它以相同的方式显示,或者我收到错误。

编辑:

如果我添加ORDER BY或GROUP BY,我会收到此错误:

Trying to get property of non-object

在这行代码上:

if ($result->num_rows > 0)

2 个答案:

答案 0 :(得分:1)

更新:删除SQL注入

尝试以下内容,您的原始代码有一个外部循环似乎是问题

<?php
if(isset($_POST['sortBtn']))
{
    $sortEvent = $_POST['sort_event'];
    $sortQuery = "SELECT * FROM tbl_comment WHERE event_topic = ?";
    $sortQuery->bind_param('s', $_POST['sort_event']);
    $sortResult = mysqli_query($dbcon, $sortQuery);

    if ($sortResult->num_rows > 0) 
    {
        echo "<table class='tbEvents' border='1'>";
        echo "<tr>";
            echo "<th class='tHead'>#</th>";
            echo "<th class='tHead'>User's Name</th>";
            echo "<th class='tHead'>Event Topic</th>";
            echo "<th class='tHead'>Comment</th>";
            echo "<th class='tHead'>Date / Time</th>";
        echo "</tr>";
        while($row = mysqli_fetch_assoc($sortResult))
        {
             echo "<tr>";
                 echo "<td class='tData'>". $row["user_number"] ."</td>";
                 echo "<td class='tData'>". $row["user_nickname"] ."</td>";
                 echo "<td class='tData'>". $row["event_topic"] ."</td>";
                 echo "<td class='tData'>". $row["user_comment"] ."</td>";
                 echo "<td class='tData'>". $row["date_time"] ."</td>";
             echo "</tr>";
         }
        echo "</table>";
    } else {
        echo "0 results";
    }
}
?>

答案 1 :(得分:1)

当您运行SELECT event_topic FROM tbl_comment时,它会获取表格中的所有记录(之后您应该使用WHERE进行手动比较。)

表格中有6行。当$sortEvent不是ABC时,循环将立即退出;这意味着你的循环运行了3次,这意味着<table>也会显示3次。

您可以完全摆脱外部循环并使用下面的查询代码,这将阻止SQL Injections

        $query = "SELECT * FROM tbl_comment WHERE event_topic = ?";
        $query->bind_param('s', $_POST['sort_event']);
        $result = mysqli_query($dbcon, $query);