gpt4 book ai didi

php - 检索数据库表名并打印出来

转载 作者:搜寻专家 更新时间:2023-10-30 21:42:58 24 4
gpt4 key购买 nike

这应该相当简单。我设计了一个 SQL 查询,将所有匹配的数据库表名转储到一个数组中。我想打印出来,但出于某种原因我不能。请帮忙!

数据库表被命名为:example_1、example_2 等。

我的代码:

$sql = "SHOW TABLES LIKE 'example_%'";
$results = $wpdb->get_results($sql);

执行 print_r($results) 显示所有表名都已成功检索。示例输出:

Array ( [0] => stdClass Object ( [Tables_in_wordpress (example_%)] => example_1 ) [1] => stdClass Object ( [Tables_in_wordpress (example_%)] => example_2 ) )

然后我尝试了:

foreach($results as $res) {
echo $res;
}

但运气不好。如何遍历 $results 以打印出每个表名?是否有类似 $results[0].value 的东西可以用来检索值?

最佳答案

我相信这是你需要做的:

$sql = "SHOW TABLES LIKE '%'";
$results = $wpdb->get_results($sql);

foreach($results as $index => $value) {
foreach($value as $tableName) {
echo $tableName . '<br />';
}
}

=> 将键和值从多维数组中分离出来。

关于php - 检索数据库表名并打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com