0

I created a custom table for my Wordpress site using Sequel Pro. I am then trying to query the table from Wordpress PHP file using $wpdb method. While I am able to run a successful query using the standard Wordpress tables, it does not work for the custom tables I create.

So this one works:

<?php
global $wpdb;

$result = $wpdb->get_results ( "
    SELECT * 
    FROM  $wpdb->users
        WHERE ID = '1'
" );

foreach ( $result as $page )
{
   echo $page->user_email.'<br/>';
   echo $page->user_nicename.'<br/>';
}
?>

This one doesn't:

<?php
global $wpdb;

$result = $wpdb->get_results ( "
    SELECT * 
    FROM  $wpdb->prefix.'my_employees'
        WHERE ID = 1
" );

foreach ( $result as $page )
{
   echo $page->user_login.'<br/>';
   echo $page->user_nicename.'<br/>';
}
?>

What am I doing wrong?

1 Answer 1

2

You have a double quoted string, so your table name literally contains .'my_employees'

Try the curly syntax instead:

"SELECT * 
FROM {$wpdb->prefix}my_employees
WHERE ID = 1
";
0

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.