WordPress.org

Ready to get started?Download WordPress

Forums

How to get a custom table in the wordpress database (4 posts)

  1. jkmuller
    Member
    Posted 6 years ago #

    I've placed the following function in my functions.php file. I am trying to get data out of a table I created in the wordpress database. It's giving me this error when I try to call it: Fatal error: Call to a member function get_results() on a non-object in C:\Program Files\wamp\www\wordpress\wp-content\themes\Theme 1\functions.php on line 8

    Line 8 is the $themes = ...

    function theme_query () {    
    
    	$querystr = "SELECT * FROM custom_themes";
    	$themes = $wpdb->get_results($querystr, OBJECT);
    
    foreach ($themes as $themes) {
    	echo $themes->title;
    }
    }

    What am I doing wrong?

  2. pizdin_dim
    Member
    Posted 6 years ago #

    You're missing the "global $wpdb;" declaration at the top of your function.

  3. jkmuller
    Member
    Posted 6 years ago #

    Looks like I needed to add a:

    global $wpdb;

    So, the code should be:

    function theme_query () {    
    
    global $wpdb;
    
    	$querystr = "SELECT * FROM custom_themes";
    	$themes = $wpdb->get_results($querystr, OBJECT);
    
    foreach ($themes as $themes) {
    	echo $themes->title;
    }
    }
  4. jkmuller
    Member
    Posted 6 years ago #

    I swear I didn't steal it from you pizdin! Thanks for replying though.

Topic Closed

This topic has been closed to new replies.

About this Topic