1

I have this code

$marker = 'werkz'; $sql = "SELECT name, marker FROM sidebar"; $q = $db->query($sql); $q->setFetchMode(PDO::FETCH_ASSOC); while ($r = $q->fetch()) { echo'<option>' . $r[name] . '</option>'; } 

It works but when I add WHERE marker = $maker; the query fails.

What is the problem?

3
  • What datatype is marker--varchar? It needs to be encapsulated in quotes... Commented Sep 9, 2012 at 15:57
  • 1
    Fail as in, throws error or doesnt return results? Commented Sep 9, 2012 at 16:00
  • conisder using try-catch to catch PDO-mysql errors. Commented Sep 9, 2012 at 16:05

2 Answers 2

4

since you are using PDO, do it like this when passing parameter.

$marker = 'werkz'; $sql = "SELECT name, marker FROM sidebar WHERE marker = ?"; $q = $db->query($sql); $q->bindParam(1, $maker); $q->setFetchMode(PDO::FETCH_ASSOC); while ($r = $q->fetch()) { echo'<option>' . $r[name] . '</option>'; } 
Sign up to request clarification or add additional context in comments.

Comments

0

$market is a string. So you should put it between '

something like ... where marker='".$marker."'";

5 Comments

-1 the user is already using PDO. you are making it vulnerable with SQL injection. don't concatenate the value but instead bind it as parameter.
I have no idea on what PDO is. He asked why the where does not work and I answered. Bu usually smart guys like you fail to see the question ..
your suggestion works thank you for that. indeed security can be an issue i have to look at that.
If your marker is an outside your application string then the use of PDOs way should be the way to do it. I just answered to your question, and that is why it does not work.
As a side note, I find a reply like: Do it like this because this is the way not helpful, as it does not help to understand why it does not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.