在php中列出1行而不是所有行

时间:2013-11-20 20:20:51

标签: php mysql sql

出于某种原因,当我想要的是返回一个等于下拉列表中所选项目的单行时,这将返回member_details表中的所有内容。

这是它的php:我认为它将返回等于下拉值的行,但它只列出了所有内容。

    <?php
        if (isset($_POST['members'])) {
        $ResultSet = getTableResults("member_details");
        echo "<h1> Member Details </h1>";
        echo "<table border='1' cellpadding='6'>";
        echo "<tr> <th>Id</th> <th>Name</th> <th>Job</th> <th>Wage</th> <th>Hobby</th> ";

        foreach ($ResultSet as $row) {
            echo "<tr>";
            echo "<td>" . $row ['member_id'] . "</td>";
            echo "<td>" . $row['first_name'] . " " . $row ['second_name'] . "</td>";
            echo "<td>" . $row['job'] . "</td>";
            echo "<td>" . $row['wage'] . "</td>";
            echo "<td>" . $row['hobby'] . "</td>";
            echo "</tr>";
        }

        echo "<table>";
    }
    ?>

如果您想查看更多代码以更好地理解它,请询问并编辑并更新问题。

 function getTableResults() {
    $sql = "SELECT DISTINCT member_details.member_id, members.first_name,      members.second_name, member_details.wage, member_details.job, member_details.hobby
       FROM members
       INNER JOIN member_details
       ON members.member_id=member_details.member_id";
    $mysqlConnection = getSQLConnection();

    $ResultSet = $mysqlConnection->query($sql);
    return $ResultSet;
  }

1 个答案:

答案 0 :(得分:3)

您在请求信息时没有任何条件,getTableResults()会获取存储在您桌面上的所有数据。您需要更改该功能并添加WHERE xy条件。可能是这样的:

 function getTableResults( $id ) {
    $sql = "SELECT DISTINCT member_details.member_id, members.first_name,members.second_name, member_details.wage, member_details.job, member_details.hobby
       FROM members
       INNER JOIN member_details
       ON members.member_id=member_details.member_id WHERE member_details.member_id = '$id'";
    $mysqlConnection = getSQLConnection();

    $ResultSet = $mysqlConnection->query($sql);
    return $ResultSet;
  }

然后使用requred member-id作为参数调用该函数。 无论如何,我建议你告诉自己有关预防SQL注入的预备语句!