【问题标题】:How can we fetch all rows of a table in an array? [duplicate]我们如何获取数组中表的所有行? [复制]
【发布时间】:2015-02-16 20:34:57
【问题描述】:

我们如何获取数组中表的所有行? 此表未通过 php PDO 连接 到目前为止,我尝试了以下返回结果集的最后一行。

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

    $results= $sth->fetch();

    print_r($results);

【问题讨论】:

  • 要访问存储在行中的数据,您可以使用 DQL 命令SELECT

标签: php pdo


【解决方案1】:

你可以使用fetchAll

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

var_dump($results);

【讨论】:

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

    【讨论】:

      【解决方案3】:
      $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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-20
        • 2023-01-25
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        相关资源
        最近更新 更多