使用PDO获取一列

时间:2011-11-21 17:45:19

标签: php pdo

我有以下PDO声明:

$stmt = $db->prepare("SELECT MAX(RID) FROM TEMP_ROUTE");
                                 $stmt->execute();
                                 $rid = $stmt->fetch(PDO::FETCH_ASSOC);
                                 $rid = (int) $rid["MAX(RID)"];

有没有办法让我不必在那里做最后的陈述。换句话说,我希望它在执行提取时获取$rid["MAX(RID)"]

1 个答案:

答案 0 :(得分:3)

看看http://www.php.net/manual/en/pdostatement.bindcolumn.php并尝试使用PDO :: FETCH_BOUND吗?但这不会缩短。或者从http://www.php.net/manual/en/pdostatement.fetch.php

尝试这样的事情
$stmt = $db->prepare("SELEXT MAX(RID) AS max FROM TEMP_ROUTE");
$stmt->execute();
$rid = $stmt->fetch(PDO::FETCH_OBJ)->MAX;