问题
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