Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a code Example:

function custom_func(){
    global $wpdb;
    $wpdb->flush();     //tried with and without this line
    $getTest = 'SELECT * FROM $wpdb->wp_users LIMIT 1';
    $arrayReturned = $wpdb->get_results($wpdb->prepare($getTest));
}

From what I've read I thought that $wpdb->wp_users is meant to have returned the database name and table name like so dbName.tableName; but it just returns an empty value.

I've tried:

$getTest = 'SELECT * FROM $wpdb->wp_users LIMIT 1';

which shows as the following to wordpress:

SELECT * FROM $wpdb->wp_users LIMIT 1

and

$getTest = 'SELECT * FROM '.$wpdb->wp_users.' LIMIT 1';

which shows as the following to wordpress:

SELECT * FROM  LIMIT 1

I can't fathom why this isn't working since this is all based on literature from the wordpress codex, any thoughts?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

First of all why you would need custom query for this basic functionality to get users, when WordPress has inbuilt function get_users().

Anyway for custom query, table name is defined as

function custom_func(){
 global $wpdb;
 $getTest = "SELECT * FROM $wpdb->users LIMIT 1";
 $arrayReturned = $wpdb->get_results($wpdb->prepare($getTest));
}

It's $wpdb->users or $wpdb->post and not $wpdb->wp_users, what you use for a table name, in custom queries.

Also I've used the double quotes " " for query and not single quotes ' ', you can read here

share|improve this answer
    
This was just a test example based on what I was reading, but you fixed all of the problems I was having with the table name changes, thanks. –  VikingGoat Mar 26 at 17:17
    
Hope you could commit to community too :) –  sven Mar 26 at 20:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.