I have created a simple html form to submit a value to a php script I have written. I am using wordpress. I want the script to look up a user in the user_meta table, and select the user ID. I then want the script to look up the email of that user in the wp_users table. Here is the code I have:
<?php
global $wpdb;
$userid = $wpdb->get_var ( $wpdb->prepare (
"
SELECT user_id
FROM $wpdb->user_meta
WHERE user_meta = %s
",
$_POST["submitted_value"] ) );
$useremail = $wpdb->get_var (
"
SELECT user_email
FROM $wpdb->wp_users
WHERE ID = %s
"
, $userid );
echo 'Car Owner Email is' . $useremail . '!';
echo "test" . $_POST["submitted_value"] ."!";
?>
Now, I have run the script and the last line appears to be working — it is printing the ‘submitted_value’ from my html form – but it does not seem to be querying the database.
this is the entire php script — I am placing it in the wordpress plugins directory — do I need to add anything else to get it to query my wordpress database?
In WHERE Clause, you need to give a meta key with which the value has to be mapped. Ex: WHERE meta_key = ‘nickname’ and meta_value = ‘pranita’.
Replace ‘your_meta_key_name’ with proper key with which you are matching your form value.
If you don’t understand fields, check wp_usermeta table in phpmyadmin.
Hope this helps. All The Best
You should update your code:
But you can do it easily following the WP codex: http://codex.wordpress.org/Function_Reference/get_users/
In this case you will get all possible users and you may need to run a loop.