2011-10-24 41 views
0

如果我從我SQLite數據庫的主表中的記錄是這樣的:如何讓SQLite/PDO-> query()只返回指定的字段?

$database = new PDO('sqlite:test.sqlite'); 
$sql = "SELECT * FROM sqlite_master"; 
$records = $database-> query($sql); 

然後它給我的每一個記錄翻一番場的數量,既能索引和關聯:

type:[table] 
0:[table] 
name:[site_comments] 
1:[site_comments] 
tbl_name:[site_comments] 
2:[site_comments] 
rootpage:[3] 
3:[3] 
sql:[CREATE•TABLE•site_comments(author•varchar(50),•comment•varchar(255))] 
4:[CREATE•TABLE•site_comments(author•varchar(50),•comment•varchar(255))] 

我記得使用許多年前的PDO對象和fetchAll()等。方法,這是你如何確定,如果你想要聯想或索引字段,或一個或另一個,等等。但這似乎是一個新的/不同版本的PDO(PHP與最新版本的XAMPP安裝):

enter image description here

如何告訴PDO對象,我希望它只給出指定的數組,例如「type」和「name」,但不是0和1?

回答

0

可以擴展PDO類本身這樣每次使用query()方法時,它調用setFetchMode是你如何想:

class PDO_Ex extends PDO { 

    function query($query){ 

     $statement = parent::query($query); 
     $statement->setFetchMode(PDO::FETCH_ASSOC); 
     return $statement; 
    } 

} 
相關問題