我有$ pdo-> execute($ values)的問題。pgsql 42601錯誤與PDO ::執行
如果我用$pdo->execute()
它工作正常,無$值:
$dsn = sprintf('%s:host=%s;port=%s;dbname=%s', DB_TYPE, DB_HOST, DB_PORT, DB_NAME);
$pdo = new PDO($dsn, DB_USER, DB_PASS, array());
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM "user" ORDER BY id DESC LIMIT 50 OFFSET 0';
$stmt = $pdo->prepare(trim($sql));
$stmt->setFetchMode(PDO::FETCH_OBJ|PDO::FETCH_PROPS_LATE);
$stmt->execute();
echo "<pre>";
print_r($stmt->fetchAll());
echo "</pre>";
它工作正常,但是當我要添加值替換:
$dsn = sprintf('%s:host=%s;port=%s;dbname=%s', DB_TYPE, DB_HOST, DB_PORT, DB_NAME);
$pdo = new PDO($dsn, DB_USER, DB_PASS, array());
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM "user" ORDER BY ? ? LIMIT 50 OFFSET 0';
$values = array('id','DESC');
$stmt = $pdo->prepare(trim($sql));
$stmt->setFetchMode(PDO::FETCH_OBJ|PDO::FETCH_PROPS_LATE);
$stmt->execute($values);
echo "<pre>";
print_r($stmt->fetchAll());
echo "</pre>";
它不工作。它返回:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "$2" LINE 1: SELECT * FROM "user" ORDER BY $1 $2 LIMIT 50 OFFSET 0 ^' in ...
沒有意識到你實際上可以使用綁定在order by子句瓦爾,我還以爲是像瓦爾綁定爲一個表名稱 – 2012-01-08 23:51:25