从MySQL表中获取多个结果并将它们放在HTML表(PHP / MySQLi)中:

时间:2015-06-15 21:58:32

标签: php mysqli

好的,所以我经过一段很长的休息时间回到整个网络开发,并且有点生疏;我正在研究一些旧的项目,其中一个我正在创建一个消息系统。关于如何继续,我有点迷茫。

public function loadMessages() {
        $UAC_base = new UAC_base();
        $global_functions = new globalFunctions();
        $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        $user = $global_functions->convertUsernameToID($_SESSION["user_name"]); 
        $checkUserMessages_query = "SELECT ucp_message_sentby, ucp_message_subject, ucp_message_content, ucp_message_datetime FROM csgoplace_messages WHERE ucp_message_target = '" . $user . "';";
        $checkUserMessages_query_result = $this->db_connection->query($checkUserMessages_query);
        $result_row = $checkUserMessages_query_result->fetch_object();
        $num_rows = $checkUserMessages_query_result->num_rows;

        if($checkUserMessages_query_result->num_rows > 0) {
            $msg_sentby = $global_functions->convertUserIDToName($result_row->ucp_message_sentby);
            $msg_subject = $result_row->ucp_message_subject;
            $msg_content = $result_row->ucp_message_content;
            $msg_datetime = $result_row->ucp_message_datetime;

            echo "
            <tr>
                <td>" . $msg_sentby . "</td>
                <td>" . $msg_subject . "</td>     
                <td>15 June 2015, 13:56PM</td>
                <td><a href='#'>View</a> | <a href='#'>Delete</a>
            </tr>";

        }

这就是我目前所拥有的,它更像是一种测试方法,以确保我的表格和查询等格式都很好,但当然,这只会产生一个结果。如何使其检索所有结果并使用所述结果填充整个HTML表?

1 个答案:

答案 0 :(得分:0)

我不知道你的类看起来如何,但$checkUserMessages_query_result->fetch_object();PDOStatement::fetch(PDO::FETCH_OBJ)的包装器 你应该将这行代码放入while循环中,如下所示:

while($result_row = $checkUserMessages_query_result->fetch_object()){
    //do stuff with $result_row
} 

如果没有(或没有)可用行,PDOStatement::fetch()方法将返回false。