• If i have created my own table in the wp_ database can anyone point me in the right direction as how to query the database to get info out of it?

    Can I use $wpdb ?

    Any help would be appreciated thanks!!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter maestro42

    (@maestro42)

    i have already been through that page many, many times! Does $wpdb work with a table that I added myself?

    For example, my table is called scoreboard.
    So I was using, $wpdb->prefix . “scoreboard”

    Any thoughts? Thanks.

    Thread Starter maestro42

    (@maestro42)

    This is what I have been trying:

    $table_name = $wpdb->prefix . "scoreboard";
    $mylink = $wpdb->get_row("SELECT * FROM $wpdb->prefix . 'scoreboard' WHERE id = 0");
    echo $mylink->id;

    Thread Starter maestro42

    (@maestro42)

    any ideas?

    Have you tried:
    $mylink = $wpdb->get_row("SELECT * FROM scoreboard WHERE id = 0");
    I’ve used a similar method to get the most recent 5 Titles & Dates of posts from 1 blog to show on a page in a separate WordPress installation/website (but with the tables of both blogs being in the same database) using this:
    $stitle = $wpdb->get_results("SELECT post_title, post_date_gmt FROM table_name_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date_gmt DESC LIMIT 0,5");
    and then used a foreach() command to show the details retrieved from each row that is chosen.

    Thread Starter maestro42

    (@maestro42)

    okay. this is what I am trying now:

    $mylink = $wpdb->get_results("SELECT * FROM scoreboard WHERE id = 0");
    echo $mylink;

    It just prints “Array”. What am I missing here?

    Thanks.

    It prints “Array” because $wpdb->get_results() returns an array of the results.

    try:

    print_r($mylink);

    or, I guess:

    echo $mylink[0]->id

    Thread Starter maestro42

    (@maestro42)

    Okay here is what I have:

    $table_name = $wpdb->prefix . "scoreboard";
    $sql = "SELECT * FROM " . $table_name . " WHERE id = 2";
    $pageposts = $wpdb->get_results($sql, ARRAY_N);
    print_r($pageposts);
    echo $pageposts[0]['team1'];

    When the print_r() function prints out I get the following:

    Array ( [0] => Array ( [id] => 2 [team1] => Los Angeles [team1_score] => 0 [team2] => New Orleans [team2_score] => 0 [time] => 12:00 [status] => Qtr 1 ) )

    At first I was using :
    echo $pageposts['team1'];
    to try and print out the team name, then I saw the double Array so I tried
    echo $pageposts[0]['team1'];
    and that worked.

    Can someone explain to me if this is right? Should there be a double array like that? It didn’t seem right to me. Thanks

    Thread Starter maestro42

    (@maestro42)

    This doesn’t seem like the right behavior to expect. Can anyone shed some light if I am doing things correctly ?

    $wpdb->get_results returns an array of rows from the database (in your case, just one row), and each one of those rows is another array… so yes… it is right.

    $wpdb->get_results returns an array so you wanna loop the results out:

    $pageposts = $wpdb->get_results($sql, ARRAY_N);
    while($row = mysql_fetch_array($pageposts)) {

    echo $row[‘fieldname’];

    }

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘how to query database?’ is closed to new replies.