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
  • 156,878
  • 40
  • 214
  • 345
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,361
  • 1
  • 20
  • 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
  • 2
  • 12
0
$sth = $dbh->query("SELECT * FROM schema.TABLES WHERE TABLE_SCHEMA = 'dbname'"); 
while ($row = $sth->fetch(PDO::FETCH_ASSOC)){
    print_r($row);
}
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91