Axxess
Axxess

Reputation: 532

Query fails when I use where

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?

Upvotes: 1

Views: 76

Answers (2)

John Woo
John Woo

Reputation: 263693

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>';
}

Upvotes: 4

Dumitrescu Bogdan
Dumitrescu Bogdan

Reputation: 7267

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

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

Upvotes: 0

Related Questions