检索数据库表名称并打印出来 [英] Retrieve database table names and print them out

查看:91
本文介绍了检索数据库表名称并打印出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This should be fairly simple. I've crafted an SQL query that dumps all matching database table names to an array. I want to print them out, yet for some reason I cannot. Please help!

数据库表的命名为:example_1,example_2等。

Database tables are named: example_1, example_2, etc.

我的代码:

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

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

Doing a print_r($results) shows that all the table names were successfully retrieved. Example output:

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 的值?

But no luck. How can I iterate through $results to print out each table name? Is there something like $results[0].value I could use to retrieve the values?

推荐答案

我相信这是您需要做的:

I believe this is what you need to do:

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

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

The =>将键和值与多维数组。

The => will separate the key and value from a multidimensional array.

这篇关于检索数据库表名称并打印出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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