admin管理员组

文章数量:1336632

I have this little routine to find the user_id based on a usermeta field..

$scaleData = json_decode($reading, TRUE);
$deviceid = $scaleData["imei"];
echo $deviceid; // check to confirm is working. Yup

$WhoIsUser = get_users(
  array(
   'meta_key' => 'deviceid',
   'meta_value' => '$deviceid'
 )
);

$CurrentUser = $WhoIsUser[0]->ID;
echo $CurrentUser; //returns nothing

But if I switch 'meta_value' => 45455 (iow a known device number) it returns the ID no problem. I've tried both $deviceid and '$deviceid' What am I missing?

I have this little routine to find the user_id based on a usermeta field..

$scaleData = json_decode($reading, TRUE);
$deviceid = $scaleData["imei"];
echo $deviceid; // check to confirm is working. Yup

$WhoIsUser = get_users(
  array(
   'meta_key' => 'deviceid',
   'meta_value' => '$deviceid'
 )
);

$CurrentUser = $WhoIsUser[0]->ID;
echo $CurrentUser; //returns nothing

But if I switch 'meta_value' => 45455 (iow a known device number) it returns the ID no problem. I've tried both $deviceid and '$deviceid' What am I missing?

Share Improve this question asked May 20, 2020 at 22:41 Johnnyboy GomezJohnnyboy Gomez 333 bronze badges 2
  • I think you should remore the quotes around $deviceid, that should be the reason why it is not getting a value. With the quotes he is not loading the value of the variable, but just the string. But you said you already tried that... have you tried outputting the value to see if the Id is correctly saved in the variable you use as meta_value? – rank Commented May 20, 2020 at 22:53
  • Got it! I needed to use "double-quotes". Such a simple mistake and hours lost! Thx your comment got me thinking on the right path. – Johnnyboy Gomez Commented May 20, 2020 at 23:24
Add a comment  | 

1 Answer 1

Reset to default 0

Needs double quotes

$WhoIsUser = get_users(
  array(
   'meta_key' => 'deviceid',
   'meta_value' => "$deviceid"
 )
);

本文标签: meta querygetuser can39t read variable