1
votes

I am new to programming and following instructions in a couple of books and the wordpress codex. I am attempting to access a custom database using the $wpdb object. This is being done locally. I get the following error.

Notice: Undefined property: wpdb::$national_re_trends in C:\wamp\www\wordpress\wp-includes\wp-db.php on line 68

Also get this:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1] SELECT * FROM

Here is the query:

global $wpdb;                       
$stuff = $wpdb->get_results("SELECT * FROM $wpdb->national_re_trends" );
print_r($stuff);

Here is my database table:

Database table

I am sure this is some newb stuff but if someone could help, I would be grateful!

2
this may provide some help stack exchange using wpdb - dpp

2 Answers

0
votes

There is no "built-in" WordPress table referred to as national_re_trends, so $wpdb->national_re_trends will not exist. (Here is a list of the 11 built-in WP tables).

Instead, you have to access the table via the name you've assigned to it.

Typically, I like to define a constant, something like so so that it gets the table prefix built into it (of course, if you didn't include the table prefix when you created the table, you'd want to take that into consideration and remove the $table_prefix below).

global $table_prefix;
define( 'MY_TABLE_NAME', $table_prefix . 'national_re_trends' );

Then, your query would look something like so:

global $wpdb;
$stuff = $wpdb->get_results( 'SELECT * FROM ' . MY_TABLE_NAME );
var_dump( $stuff );
0
votes

If dhe_ is your table prefix, remove $wpdb after the FROM.

  global $wpdb;                       
  $stuff = $wpdb->get_results("SELECT * FROM dhe_national_re_trends" );
  print_r($stuff);

If you want to use $wpdb->prefix

global $wpdb;                       
  $stuff = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}national_re_trends" );
  print_r($stuff);