1

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);
Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
eddywebs
  • 93
  • 1
  • 12

3 Answers3

1

You can use fetchAll

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

var_dump($results);
fortune
  • 3,293
  • 1
  • 19
  • 30
0
$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);
Michael Freund
  • 234
  • 1
  • 11
0
$sth = $dbh->query("SELECT * FROM schema.TABLES WHERE TABLE_SCHEMA = 'dbname'"); 
while ($row = $sth->fetch(PDO::FETCH_ASSOC)){
    print_r($row);
}
Deepu
  • 11,587
  • 13
  • 55
  • 88