Reputation: 99
In my WordPress project, I have a table named products, in that for selecting all rows in the product table as of now I have used
$table_name = $wpdb->prefix . "products";
$output = $wpdb->get_results( "SELECT * FROM $table_name");
so now my question is instead of writing the table name with prefix, how can I use like this, wpdb->posts
in that posts will refer to the table named posts in WordPress table
Upvotes: 0
Views: 596
Reputation: 9401
This can be easily fixed by replacing the wp_
string with actual properties provided by using the prefix.
For example, assuming that the table prefix is wp_
:
$wpdb->posts
will correspond to wp_posts
table
$wpdb->postmeta
will correspond to wp_postmeta
table
$wpdb->users
will correspond to wp_users
table
For more please visit this link. Reference URL link
Note: If you create a custom table without prefix then you do not need to use $wpdb
class. It is just used to get WordPress database default prefix or for wp_query
AND for others queries.
If you want to run more than one WordPress in a single database then provide prefix otherwise you can skip it.
Upvotes: 0
Reputation: 1624
You can use this code,
global $wpdb;
$table_name = $wpdb->prefix . "products";
$output = $wpdb->get_results( "SELECT * FROM $table_name");
Upvotes: 0
Reputation: 1213
try this:
$table_name = "products";
$output = $wpdb->get_results( "SELECT * FROM $table_name");
Upvotes: 2