简体   繁体   English

显示表仅获取表的姓氏

[英]show tables get only the last name of table

I'm trying to show the name of table in my database. 我正在尝试在数据库中显示表的名称。 I write this code : 我写这段代码:

   function affiche_liste()
{   
$db=new PDO('mysql:host=localhost;dbname=testf','root','');
    $result = $db->query("SHOW TABLES");
       foreach($result->fetch(PDO::FETCH_NUM) as $data) {
            $tableList = $data[0];
        }
        return $tableList;
}

It give to me only the last table ? 它只给我最后一张桌子吗?

For a simple query without parameters and the SQL hard coded you can use a generic function passing the connection and SQL to the function. 对于不带参数和SQL硬编码的简单查询,可以使用将连接和SQL传递给该函数的通用函数。 The following function () returns an array containing all rows in the result set. 以下函数()返回一个数组,其中包含结果集中的所有行。

function queryAll($db,$query){  
    $sth = $db->query($query);
    $result = $sth->fetchAll(PDO::FETCH_NUM);
    return $result;
}

For a simple query without parameters and the SQL hard coded you can use a generic function passing the connection and SQL to the function. 对于不带参数和SQL硬编码的简单查询,可以使用将连接和SQL传递给该函数的通用函数。 The following function () returns an array containing all rows in the result set. 以下函数()返回一个数组,其中包含结果集中的所有行。

function queryAll($db,$query){  
    $sth = $db->query($query);
    $result = $sth->fetchAll(PDO::FETCH_NUM);
    return $result;
}

$db=new PDO('mysql:host=localhost;dbname=testf','root',''); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SHOW TABLES";
$tables = queryAll($db,$query);
print_r($tables);   

Each time you are looping through the results, your are overwriting the variable tableList . 每次循环搜索结果时,都会覆盖变量tableList Instead, you need to append to an array of results. 相反,您需要附加到结果数组。

function affiche_liste()
{   
    $db=new PDO('mysql:host=localhost;dbname=testf','root','');
    $result = $db->query("SHOW TABLES");
    $tableList = array();
    foreach($result->fetch(PDO::FETCH_NUM) as $data) {
        array_push($tableList, $data[0]);
    }
    return $tableList;
}

try this ` 试试这个`

function affiche_liste()
{$db=new PDO('mysql:host=localhost;dbname=testf','root','');
    $result = $db->query("SHOW TABLES");
       foreach($result->fetch(PDO::FETCH_NUM) as $data) {
            $tableList[] = $data[0];
        }
        return $tableList;
}

` `

i hope it'll work... here your array is overwitting every time...that's the reason you were getting the last table name....so you need to append to the existing array... 我希望它能起作用...在这里每次您的数组都被覆盖...这就是您获取最后一个表名的原因....所以您需要追加到现有的数组中...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM