以下是我的PHP代码的摘录:
从PostgreSQL数据库打印查询结果的函数:
function getOuvrage(){
$conn = PostgreConnection::getInstance();
$sth = $conn->prepare("SELECT * FROM ouvrage WHERE code = '01'");
$sth->execute();
$result = $sth->fetchAll();
print_r($result);
}
结果pgAdminIII中的SQL查询是:
code |forage | station | reserve
---------+--------+-----------+----------
01 | 2 | 12 | 87
print_r()
功能的结果是:
Array([0] => Array ([forage] => 2 [0] => 2 [station] => 12 [1] => 12 [reserve] => 87 [2] => 87))
我的问题:为什么有2个数组,我怎么能得到这样一个简单的数组:
Array([forage] => 2 [station] => 12 [reserve] => 87)
编辑:
我试过了:
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
我仍然得到2个数组:
Array([0] => Array([forage] => 2 [station] => 12 [reserve] => 87))
我试过了:
$result = $sth->fetchAll(PDO::FETCH_KEY_PAIR);
我明白了:
Array()
我无法找到问题的答案,包括@Your Common Sense指出的“重复”问题!
答案 0 :(得分:0)
解决方案是:
$result = $sth->fetch(PDO::FETCH_ASSOC);