Support » Fixing WordPress » Retrieving data from a database

  • 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

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

    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>";
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Retrieving data from a database’ is closed to new replies.