Title: WPDB
Last modified: August 19, 2016

---

# WPDB

 *  Resolved [chiconerd](https://wordpress.org/support/users/chiconerd/)
 * (@chiconerd)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/)
 * alright so i was looking at how to manage queries in wordpress and i came across
   wpdb, it says that it should work just fine with no modification.
 * but when i try to use a query it gives me a fatal error, and tells me im working
   with a non-object..
 * i try many different things and the only thing that works is if i call the class
   and put in all the database values manually every time..
    this a security risk..
 * can anyone help me?
 * why is not working like the guide says it should.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977605)
 * What are you trying to query?
 * While [wpdb](http://codex.wordpress.org/wpdb) may be useful, if you are querying
   posts you might look at the [template tag](http://codex.wordpress.org/Template_Tags),
   [query_posts()](http://codex.wordpress.org/Template_Tags/query_posts), or [get_posts()](http://codex.wordpress.org/Template_Tags/get_posts).
 *  Thread Starter [chiconerd](https://wordpress.org/support/users/chiconerd/)
 * (@chiconerd)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977688)
 * nope just a normal query
    when i use this
 *     ```
       <?php
           $wpdb = $wpdb->get_results('SELECT * FROM costume_table');
       ?>
       ```
   
 * this will produce this error
 *     ```
       Fatal error: using a property of a non-object;
       ```
   
 * how ever if i do this
 *     ```
       <?php
           $wpdb = new wpdb('user','password','database','host');
           $wpdb = $wpdb->get_results('SELECT * FROM costume_table');
       ?>
       ```
   
 * and that is the problem i have to include the new class in every new page i make,
   i thought the class was included within wordpress, putting mysql info in every
   page does not sound safe. :S
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977692)
 * If you put the database tables you are accessing in the save database as the 
   rest of WordPress tables you should be able to access them with the wpdb class.
 *  Thread Starter [chiconerd](https://wordpress.org/support/users/chiconerd/)
 * (@chiconerd)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977695)
 * yeah the problem is not accessing the database, is running the code.
    the code
   wont run unless i create an instance of the class in every page i want to use
   the wpdb class.
 * as i explained before, in the documentation it says that automatically wordpress
   enable this functionally. but even if i try to use one example provided by wordpress
   i get this error.
 * _this the code wordpress provides_
 *     ```
       <?php
       $name = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE term_ID=1");
       echo $name;
   
       ?>
       ```
   
 * _this is the error it produces_
    `Fatal error: Call to a member function get_var()
   on a non-object in C:\sites\wordpress\wp-content\plugins\exec-php\includes\runtime.
   php(42) : eval()'d code on line 2`
 *  Thread Starter [chiconerd](https://wordpress.org/support/users/chiconerd/)
 * (@chiconerd)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977698)
 * well i found another way to do this in every page i create instead of using the
   new class. by using
 *     ```
       global $wpdb;
       ```
   
 * the code works.
 * still not sure why in the guide they don’t address this error.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977735)
 * Even though [wpdb](http://codex.wordpress.org/wpdb) does refer to making $wpdb
   global, can you recommend a better method of explaining that?
 * Thanks for your recommendations.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘WPDB’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [17 years, 4 months ago](https://wordpress.org/support/topic/wpdb/#post-977735)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
