How can we fetch all rows of a table in an array? [duplicate]

左心房为你撑大大i 提交于 2019-12-11 11:34:25

问题


How can we fetch all rows of a table in an array? This table is not connected via php PDO so far Ive tried the following returns last row of the resultset.

    $sth = $dbh->query("show tables;"); //select
    $sth->setFetchMode(PDO::FETCH_ASSOC);

    $results= $sth->fetch();

    print_r($results);

回答1:


You can use fetchAll

$sth = $dbh->query("show tables;"); //select
$sth->setFetchMode(PDO::FETCH_ASSOC);
$results= $sth->fetchAll();

var_dump($results);



回答2:


$sth = $dbh->query("SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'nameOfYourDb'"); 

while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
    print_r($row);
}

print_r($results);



回答3:


$sth = $dbh->query("SELECT * FROM schema.TABLES WHERE TABLE_SCHEMA = 'dbname'"); 
while ($row = $sth->fetch(PDO::FETCH_ASSOC)){
    print_r($row);
}


来源:https://stackoverflow.com/questions/27540096/how-can-we-fetch-all-rows-of-a-table-in-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!