This is simply a syntax error by the looks of it. You are using "
to open the string, and '
to close it. Use this:
$which_column = 'phone';
$data_check = $wpdb->get_var("SELECT " . $which_column . " FROM wp_shopping_preferences WHERE wp_user_id = '$id'");
Fun fact: when using double quotes ("
), you can directed insert a variable into the string without closing it~
$which_column = 'phone';
$data_check = $wpdb->get_var("SELECT $which_column FROM wp_shopping_preferences WHERE wp_user_id = '$id'");
EDIT
I noticed that you have $id
at the end of the query also being added. **If this is a user-submitted valueyou must use [
$wpdb->prepare`]1 to ensure it’s sanitized properly. See below:
$which_column = 'phone';
$data_check = $wpdb->get_var(
$wpdb->prepare(
"SELECT $which_column FROM wp_shopping_preferences WHERE wp_user_id = %d",
$id
)
);
The extra line breaks are unnecessary, but only to demenstrate what’s happening. I’ve replaced$id
with %d
, which will typecast $id
as a numeric value. Check out $wpdb->prepare
for more options.