I created a PDO statement with when
, then
and else
.
The only issue I have is that it doesn't want to execute from a PDO statement. If I print the entire SQL in a print_r
it shows me a perfect query. Also when I execute it on the database it runs flawless and updates the field I want.
public function sellNpcItems($items, $quantity, $npc_id)
{
try
{
$sql = "UPDATE npc_items SET available = CASE item_id";
for($i=0;$i<count($items);$i++)
{
$sql .= " WHEN ".$items[$i]." THEN available - ".$quantity[$i];
}
$sql .= " ELSE available END WHERE npc_id = ".$npc_id;
$sql = $this->dbh->prepare($sql);
$sql->execute(array($npc_id));
$this->dbh->commit();
$result = $sql->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
catch(PDOException $e){echo 'Error ! '.$e->getMessage();die();}
}
The output of above query is:
PDOStatement Object ( [queryString] => UPDATE npc_items SET available = CASE item_id WHEN 57 THEN available - 1 ELSE available END WHERE npc_id = 1 )
If I select the queryString and paste this directly in my SQL Workbench it directly updates. When I try to update it from my PHP script. It refuses to update (no error or anything) just continues like nothing happened.
Any of you have the solution?