我遇到了$ pdo->执行($ values)的问题。
如果我使用$pdo->execute()
而不使用$ values,它会正常工作:
$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]:语法错误:7错误:语法错误在“$ 2”或附近 第1行:SELECT * FROM“user”ORDER BY $ 1 $ 2 LIMIT 50 OFFSET 0 ^'in ...
答案 0 :(得分:1)
如果order by子句中有多个列,则ORDER BY需要以逗号分隔的列列表