V
V
Vasily Petrov2018-02-24 13:20:37
WordPress
Vasily Petrov, 2018-02-24 13:20:37

How to get value "A" from database where WHERE is passed by variables?

Something like this table:
5a913ac808416385058716.png
I'm trying to get a row with id 2, having the variables $bar and $hat. Let's say $bar = 5; $hat = 2; (as in the picture) and get far "A" from the second line. I do it like this:

$sql = $wpdb->get_var
($wpdb->prepare("SELECT far FROM table WHERE bar =%s", $bar, "AND hat = %s", $hat ));

But in this way I get the 1st line instead of the 2nd one, because far both there and there = "A" ?
Why is that? How can I fix it? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Epifanov, 2018-02-24
@mbpig

$wpdb->prepare("SELECT far FROM table WHERE bar = %s AND hat = %s", $bar, $hat )

here you can read

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question