简体   繁体   中英

Taking multiple results from a MySQL table and putting them in a HTML table (PHP / MySQLi):

Okay, so I'm coming back to web development as a whole after a pretty long break and am a bit rusty; I'm working on some of my older projects and for one of them I am creating a message system. I'm a bit lost as to how I need to proceed.

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>";

        }

This is what I have at the moment, it was more of a testing method to make sure that the formatting of my table and the queries etc. were fine which they were, but of course, this only yields one result. How can I make it retrieve all results AND populate the entire HTML table with said results?

I dont know how your classes look but if $checkUserMessages_query_result->fetch_object(); is a wrapper for PDOStatement::fetch(PDO::FETCH_OBJ) you should put that line of code into a while loop like so:

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

The PDOStatement::fetch() method will return false if there are no (or no more) rows available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM