I have a custom WordPress table on a WordPress MultiSite called "ihn_2_frm_item_metas" used for form entries.
Screenshot of database table is here: https://www.dropbox.com/s/ex54qxxohkexg5t/mysql.png
Right now, there are two entries. Each entry is given its own unique "item_id" (in this case, Bob Marley's entry has the id of 10 and Molly Dolly's entry has the id of 9). These entries also correspond to actual WordPress users (their username is their email address and their password(s) is "test"). The entry id is not the same number as their WordPress User Id.
I have logged in as "bob@marley.com" using the password "test."
On the home page of the blog, I am wanting to display all of items in the "meta_value" column ONLY for the currently logged in user.
I want to do this in two steps:
- Find the email address of the currently logged in user ("bob@marley.com").
- Find the "item_id" of that user's form entry (10) that is on the same row of the matching email address.
Here is the code I have come up with:
global $wpdb;
global $current_user;
get_currentuserinfo();
$email = $current_user->user_email;
$id = $wpdb->get_var( " SELECT item_id FROM ihn_2_frm_item_metas WHERE meta_value = $email " );
$client_meta = $wpdb->get_results(" SELECT * FROM ihn_2_frm_item_metas WHERE item_id = $id " );
echo '<p> $email is a ' . gettype($email) . '</p>';
echo '<p> $id is a ' . gettype($id) . '</p>';
echo '<p>The current logged in user is ' . $email . '.</p>';
echo '<p>The current form entry id is ' . $id . '.</p>';
echo "<ul>";
foreach ($client_meta as $value) {
echo '<li>' . $value->meta_value . '</li>';
}
echo "</ul>";
I have two gettype()
statements simply in order to confirm that both of my varables, $email
and $id
are strings.
Here is the output of my code:
$email is a string.
$id is a NULL.
The current logged in user is bob@marley.com.
The current form entry id is .
Note that $id
is NULL
. What confuses me is that when I change the $id
variable to this (replacing the variable $email
with 'bob@marley.com'):
$id = $wpdb->get_var( " SELECT item_id FROM ihn_2_frm_item_metas WHERE meta_value = 'bob@marley.com' " );
My output is exactly what I need:
$email is a string.
$id is a string.
The current logged in user is bob@marley.com.
The current form entry id is 10.
- Bob
- Marley
- bob@marley.com
- test
- 8
Where have I gone wrong?