Wordpress - Echo records from an SQL Query

Posted on 2011-10-22
Last Modified: 2012-05-12
Hi all,

I am using this code to create a query on wordpress:

<?php $wpdb->query('select * from myTable'); ?>

Now, how do I get it to echo the results?

From example:

echo $id;
echo $myfield;

etc from the results?

Question by:error77
    1 Comment
    LVL 70

    Accepted Solution

    You'll probably want to use get_results(), optionally with output types:

    $yourstuff = $wpdb->get_results('select * from myTable');
    foreach ($yourstuff as $yourstuff) {
            echo $id;
            echo $myfield;

    Open in new window


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
    The viewer will learn how to count occurrences of each item in an array.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now