WordPress.org

Ready to get started?Download WordPress

Forums

WP Database, show array results in table (2 posts)

  1. mitchellfr
    Member
    Posted 1 year ago #

    Hi there!

    I am trying to show some database results on a wordpress page.

    I did some research and have used this code:

    <?php
    $table_name = $wpdb->prefix . "scoreboard";
    $sql = "SELECT <code>text-51938c6978af6</code> FROM  <code>wp_fm_data_6</code> ";
    $pageposts = $wpdb->get_results($sql, ARRAY_N);
    print_r($pageposts);
    echo $pageposts[0]['team1'];
    ?>

    On my wordpress page, I get the following result:

    Array ( [0] => Array ( [0] => Testformulier ) [1] => Array ( [0] => hoihallo ) )

    "Testformulier" and "hoihallo" are the values I want to show on my page, but I want only this values in a table or something like that. So basically it have to look fancy and without the array code etc

    I was trying to get the values in a table, and tried some things but I can't make it work.

    Anybody that could help?

    (Sorry for my bad english)

  2. ianhaycox
    Member
    Posted 1 year ago #

    I don't really understand your SQL query, but maybe it's a cut-n-paste issue.

    In general to display a table of results use something like this,

    $sql = "SELECT field1,field2,field3 FROM table";
    $results = $wpdb->get_results($sql);  // return an object, not ARRAY_N
    if ($results) {
      echo '<table>';
      foreach ($results as $row) {
        // Each $row is a row from the query
        echo '<tr>';
        echo '<td>' . $row->field1; . '</td>';
        echo '<td>' . $row->field2; . '</td>';
        echo '<td>' . $row->field3; . '</td>';
        echo '</tr>';
      }
      echo '</table>';
    }

    See http://codex.wordpress.org/Class_Reference/wpdb for more info

    Also read about SQL escaping and data validation to prevent SQL injection attacks.

    Ian.

Topic Closed

This topic has been closed to new replies.

About this Topic