Reputation: 2128
I have this function
public function populate($id) {
$pdo = new PDOConfig();
$sql = "SELECT * FROM ".$this->table." WHERE `id` = :id";
$q = $pdo->prepare($sql);
$q->execute(array(":id"=>$id));
$resp = $q->fetchAll();
foreach ($resp as $row) {
foreach ($row as $key=>$value) {
if(!is_int($key))
$this->$key = html_entity_decode($value, ENT_QUOTES);
if($value == null) {
$this->$key = null;
}
}
}
$pdo = null;
unset($pdo);
}
Which works. When I try to make
$sql = "SELECT * FROM ".$this->table." WHERE `id` = :id";
into
$sql = "SELECT * FROM ".$this->table." WHERE `id` = :id AND `life` > 0";
It breaks. When I run this function in sql within terminal however, all works fine. The table has the field life and for sure the query should work. What am I missing?
Thanks
Upvotes: 0
Views: 925
Reputation: 146460
You haven't posted here neither the real SQL query generated by your PHP code nor the value of the $id
variable. This leads me to think that you haven't really inspected them. So when you say «I run this function in sql within terminal however, all works fine» you are probably referring to the SQL query you intend to create. Thus the issue is that your PHP code does not send the query and values you think it does.
Troubleshooting steps:
Right before $q->execute(array(":id"=>$id));
, inspect the values of $sql
and $id
. You can use var_dump() or your debugger of choice. In the first case, don't trust your browser rendered view; instead, use the View Source menu.
Compare those values with the expected result.
Make sure your PDO subclass captures errors. The simplest way is to make a SQL syntax error on purpose, such as XSELECT * FROM
.
Upvotes: 3