php为数据库中的每个表

时间:2017-01-08 17:30:18

标签: php sql foreach

尝试为数据库中的每个表创建一个下拉列表。

$stmt = $conn->prepare('show tables');
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $table) {
    echo $table;
}

然而,回声正在回归'数组'对于每个表而不是表名。

如何让它返回表名?

2 个答案:

答案 0 :(得分:0)

假设数组中只有一个元素,这应该可以工作:

$stmt = $conn->prepare('show tables');
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $table) {
  echo current($table);
}

答案 1 :(得分:0)

尝试$ table [0]

$stmt = $conn->prepare('show tables');
$stmt->execute();
$result = $stmt->fetchAll();

foreach ($result as $table) {
    echo $table[0] . '<br>';
}

的var_dump($结果)

array(59) {
  [0] array(2) {
    ["Tables_in_information_schema"] "CHARACTER_SETS"
    [0] "CHARACTER_SETS"
  }
  [1] array(2) {
    ["Tables_in_information_schema"] "COLLATIONS"
    [0] "COLLATIONS"
  }
  ...