PHP PDO fatal error
I have the following query:
$sql = "UPDATE db.users SET $str WHERE users.{$this->row} = {$this->value} LIMIT 1";
Which echo's out:
UPDATE db.users SET username=testUser, gid=3 WHERE users.username = mmiller LIMIT 1
However when I do:
$count = Db::init()->exec($sql);
I get:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mmiller' in 'where clause''开发者_运维技巧 in /class.php:185 Stack trace: #0 /class.php(185): PDO->exec('UPDATE db...') #1 /class.php(194): User->modify('username', 'gid', 'testUser', '3') #2 {main} thrown in /class.php on line 185
Any ideas?
You need to use "
UPDATE db.users SET username="testUser", gid="3" WHERE users.username = "mmiller" LIMIT 1
Make sure your echo shows you that. You could use '
ofcourse, whatever floats your boat :)
You need to use quotes around string values;
$sql = "UPDATE db.users SET $str WHERE users.{$this->row} = '{$this->value}' LIMIT 1";
Seeing you're already using PDO, it might be beneficial to use prepared statements. Although a bit slower for a one-off query, it increases security and handles all escaping necessary, so you won't run in to these problems.
精彩评论