1

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?

0

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

  • 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 '14 at 17:17
  • Hope you could commit to community too :) – Kumar Mar 26 '14 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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