• Hi, any idea on what′s wrong with this piece of code:

    <?php
    global $wpdb;
        $active_rows = $wpdb->get_results(
            " SELECT *
              FROM $wpdb->plugin_table
            "
        ); 
    
     foreach ($active_rows as $active_row){
            echo $active_row->the_title; 
    
        }
        ?>

    “plugin_table” is the table created at plugin install.
    “the_title” is a field in that table.

    I′m trying to collect all the titles from that table, for what it′s worth.

    Anyway, what am I doing wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Just do a var_dump of the results to see if you’re getting what you want and how it’s arranged.

    echo '<pre>'; echo var_dump( $active_rows ); echo '</pre>';

    Thread Starter smileX

    (@smilex)

    Hmm, it returns empty or NULL depending on which $wpdb-method I use.

    Those tables are not empty so I guess there is something wrong with the SQL-query.

    Do you see any errors in it? For example, I′m not sure how to handle the $wpdb->prefix since the table is created with the chosen prefix.

    Thank you for helping me out, means a lot!

    Maybe:

    function my_plugin_get_results () {
    	global $wpdb;
    	$table_name = $wpdb->prefix . "plugin_table";
    	$active_rows = $wpdb->get_results(
    		"SELECT * FROM {$table_name}"
    	);
    
    	foreach ($active_rows as $active_row){
    		echo $active_row->the_title;
    	}
    }

    As for the prefix. It depends if you used the prefix in your table name during activation.

    Thread Starter smileX

    (@smilex)

    Hi again!

    Tried out your code, at first it did not output anything at all, but when I put the function in the main plugin file it works like a charm!

    Thanks a lot!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘$wpdb->get-results not working’ is closed to new replies.