1

I can't get results from such query:

$email = $_POST['email'];
$result = $wpdb->get_row("SELECT * FROM hp_tempusers WHERE email = %", $email);

It's always empty. But if I make:

$email = '[email protected]';

everything will work. I can't find the mistake and I will be very pleasant for any help.

5
  • thanks! but it still returns null. it's not the first time I face this problem. Commented Mar 13, 2017 at 15:54
  • try this $result = $wpdb->get_row("SELECT * FROM hp_tempusers WHERE email = '$email', ARRAY_A); Commented Mar 13, 2017 at 15:56
  • with last expression it works. thanks a lot guys! Commented Mar 13, 2017 at 15:56
  • ah... no, does not work Commented Mar 13, 2017 at 16:10
  • $wpdb->prepare() this is the answer Commented Mar 13, 2017 at 19:18

2 Answers 2

1

Found a solution. This code works:

$result = $wpdb->get_row($wpdb->prepare("SELECT * FROM hp_users WHERE user_email = %s", $my_email));

Other ways do not work with dynamic string variables

Sign up to request clarification or add additional context in comments.

1 Comment

it does not matter what table and column. it did not work. and it could not work. i tried many tables and methods until understood how to do it. so i don't understand your irritation. if you don't want to waist time just relax
0

Use placeholder %s for string instead of % with $wpdb->prepare method.

$email = $_POST['email']; 
$result = $wpdb->get_row(
    $wpdb->prepare("SELECT * FROM hp_tempusers WHERE email = %s", $email)
);

Or simply use the variable within the string.

$email = $_POST['email']; 
$result = $wpdb->get_row("SELECT * FROM hp_tempusers WHERE email = '{$email}'");

4 Comments

thanks! but it's just mistake in my question. the problem is, this code $wpdb->get_row("SELECT * FROM hp_tempusers WHERE email = %s", $email); returns null
@www : try $result = $wpdb->get_row("SELECT * FROM hp_tempusers WHERE email ={$email}");
the same. Null. i think, something wrong with the string. because if i send 1 and select id, it works
the answer is to use $wpdb->prepare() in this case, otherwise it will not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.