I have the following SQL script:

$query = $wpdb->prepare(
    "SELECT user_login FROM wpdb->staff as staff
      join wpdb->users as users on staff.wp_user_id  = users.ID
       where full_name =%s",
$userlogin = $wpdb->query( $query );

But I am getting the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '->ab_staff as staff join wpdb->users as users on staff.wp_user_id = users.' at line 1

1 Answer 1


Are you sure you didn't mean $wpdb->staff? By ommitting the $, the query now looks for a table called wpdb->staff, which isn't valid SQL

As an aside, had you considered using a custom post type/user meta instead of a custom table? Or even a custom taxonomy on users?

Note as well that if you never set $wpdb->staff, it won't exist, so you'd be better using the table prefix combined with a hardcoded table name

  • You can’t access custom table names as properties on wpdb anyway, can you? Feb 7, 2018 at 8:16
  • You can if you put them there, which is what a lot of people do
    – Tom J Nowell
    Feb 7, 2018 at 13:27
  • thanks all. the following query is working: $query = $wpdb->prepare( "SELECT user_login FROM wpk6_ab_staff as staff join $wpdb->users as users on staff.wp_user_id = users.ID where full_name =%s", $usernametest ); $userlogin = $wpdb->query( $query ); but now for some reason it is returning a numeric value and not the actual user_login. Why could that be?
    – Coder321
    Feb 7, 2018 at 18:54
  • I got the answer for why it returned numeric. my code was using: $userlogin = $wpdb->query( $query ); but it should be: $userlogin = $wpdb->get_var( $query );
    – Coder321
    Feb 7, 2018 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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