只是使用此功能并且它没有按计划运行。它应该抓取数据库中的所有表名并将它们存储在一个数组中。但是,数组的结果使下面示例中显示的数组加倍:
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();
}
}
答案 0 :(得分:13)
获取表格的所有名称,这样做要好得多
public function list_tables()
{
$sql = 'SHOW TABLES';
if($this->is_connected)
{
$query = $this->pdo->query($sql);
return $query->fetchAll(PDO::FETCH_COLUMN);
}
return FALSE;
}
答案 1 :(得分:2)
您正在while循环中打印数组!每次从记录集添加项目时,都会打印它。相反,你需要在完成填充后打印它:
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();
}
}