Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have just created a custom database table and would now like to output every inserted row from it into a predefined format using a foreach statement.

Some guidance in this matter would be great.

Thanks, Ashley

share|improve this question
    
Are you using WordPress? –  Rutwick Gangurde Oct 14 '11 at 17:32
    
Yes I am. Hope you can help –  Ashley Staggs Oct 14 '11 at 17:35
    
How did you create the table? Manually? –  Rutwick Gangurde Oct 14 '11 at 17:38
    
I followed this tutorial: codex.wordpress.org/Creating_Tables_with_Plugins –  Ashley Staggs Oct 14 '11 at 17:39
    
Basically the table is filled with content when the theme is activated. (I modified the tutorial to work with themes using switch_theme –  Ashley Staggs Oct 14 '11 at 17:41
show 1 more comment

3 Answers

Assuming you have table that's prefixed with the WordPress prefix (even if it's not the default one), and the table is called table. Then the following code should select everything, and allow you to go through each row. In this example, it goes through each row and outputs the content of the field foobar.

global $wpdb;
$results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."table");
foreach ( $results as $row ) 
{
    echo $row->foobar;
};

See the Codex page on $wdpb

share|improve this answer
add comment

Use the wpdb class with something like:

global $wpdb;

$results = $wpdb->get_results("SELECT * FROM [custom table name] WHERE key = 'value'");

foreach( $results as $r ) {

    echo $r->field_name;

}
share|improve this answer
add comment
up vote 0 down vote accepted

Thanks to Rutwick Gangurde, I found out how to do it.

<?php
$mylistitems = $wpdb->get_results("SELECT * FROM wp_mytable");

foreach ( $mylistitems as $mylistitem ) 
{
echo $currency->somefield;
}
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.