WordPress.org

Ready to get started?Download WordPress

Forums

Retrieving data from a database (4 posts)

  1. Bultack
    Member
    Posted 1 year ago #

    Hello!

    I'm trying to retrieve data from a table in the database, looking in the codex I saw that I must use ezSQL and use $wpdb->get_results('query');

    The problem I have, it's that I need to show the data from the table in a XxY html table.

    I tried using ASSOC_N to use the numeric Array but I don't know how to show the data correctly, if someone can help me with that problem I will thank

  2. catacaustic
    Member
    Posted 1 year ago #

    Using the $wpdb class it's easy to loop through the results.

    $result = $wpdb->query ('SELECT * FROM tablename');
    
    foreach ($result as $row) {
        echo "<p>".$row->column_name."</p>";
    }

    Personally I don't like using numeric arrays for database queries like this because they can be very easily messed up. If osmeone adds a column into the database table, or adds another column into your query, the index that you use will be wrong. It's always a lot safer to use the assocuative indexing or the object that is returned natively.

  3. Bultack
    Member
    Posted 1 year ago #

    And how I can show the data that I retrieve from my table and put them in a html table with a 3x3 format or XxY format?

  4. catacaustic
    Member
    Posted 1 year ago #

    Just add the HTML code for each row into the foreach() loop.

    foreach ($result as $row) {
        echo "<tr>";
        echo "  <td>".$row->col1."</td>";
        echo "  <td>".$row->col2."</td>";
        echo "  <td>".$row->col3."</td>";
        echo "</tr>";
    }

Topic Closed

This topic has been closed to new replies.

About this Topic