WordPress.org

Ready to get started?Download WordPress

Forums

how to query database? (12 posts)

  1. maestro42
    Member
    Posted 6 years ago #

    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!!

  2. Samuel B
    moderator
    Posted 6 years ago #

  3. maestro42
    Member
    Posted 6 years ago #

    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.

  4. maestro42
    Member
    Posted 6 years ago #

    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;

  5. maestro42
    Member
    Posted 6 years ago #

    any ideas?

  6. mojorob
    Member
    Posted 6 years ago #

    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.

  7. maestro42
    Member
    Posted 6 years ago #

    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.

  8. wp_guy
    Member
    Posted 6 years ago #

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

    try:

    print_r($mylink);

    or, I guess:

    echo $mylink[0]->id

  9. maestro42
    Member
    Posted 6 years ago #

    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

  10. maestro42
    Member
    Posted 6 years ago #

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

  11. wp_guy
    Member
    Posted 6 years ago #

    $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.

  12. willroy
    Member
    Posted 5 years ago #

    $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'];

    }

Topic Closed

This topic has been closed to new replies.

About this Topic