PDO SHOW TABLES数组 [英] PDO SHOW TABLES array

查看:88
本文介绍了PDO SHOW TABLES数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用此功能,它并未按计划进行.它应该获取数据库中的所有表名并将它们存储在数组中.但是,数组的结果将数组加倍,如下例所示:

Just working with this function and it's not working out as planned. It is supposed to grab all table names in a database and store them in an array. However the results of the array is doubling up the array shown in the example below:

Array ( [0] => 113340 ) 
Array ( [0] => 113340 [1] => 116516 ) 
Array ( [0] => 113340 [1] => 116516 [2] => 139431 ) 
Array ( [0] => 113340 [1] => 116516 [2] => 139431 [3] => 20731 ) 
Array ( [0] => 113340 [1] => 116516 [2] => 139431 [3] => 20731 ... )

我正在使用的代码:

function itemDiscontinued($dbh, $id, $detail) {
  try {
    $tableList = array();
    $result = $dbh->query("SHOW TABLES");
    while ($row = $result->fetch(PDO::FETCH_NUM)) {
      $tableList[] = $row[0];
      print_r($tableList);
    }
  }
  catch (PDOException $e) {
    echo $e->getMessage();
  }
}

推荐答案

以获取表的所有名称,这要好得多

to get all names of the tables this is much better

public function list_tables()
{
    $sql = 'SHOW TABLES';
    if($this->is_connected)
    {
        $query = $this->pdo->query($sql);
        return $query->fetchAll(PDO::FETCH_COLUMN);
    }
    return FALSE;
}

这篇关于PDO SHOW TABLES数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆