最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how to echo all tables that start with a prefix

programmeradmin1浏览0评论

I am trying to get all tables that start with a prefix

   global $wpdb;
   $tables = $wpdb->get_results("show tables like 'pro_hist%'");
   echo '<pre>';
   print_r($tables);
   echo '</pre>;

and this works, it gives me this answer

Array
(
    [0] => stdClass Object
        (
            [Tables_in_plugin (pro_hist%)] => pro_hist1579
        )

    [1] => stdClass Object
        (
            [Tables_in_plugin (pro_hist%)] => pro_hist1580
        )
)

but how can I echo this out?

if I do print_r($tables[0]) I get this result

stdClass Object
(
    [Tables_in_plugin (pro_hist%)] => pro_hist1579
)

Now I wanted to echo "pro_hist1579" ?

I am trying to get all tables that start with a prefix

   global $wpdb;
   $tables = $wpdb->get_results("show tables like 'pro_hist%'");
   echo '<pre>';
   print_r($tables);
   echo '</pre>;

and this works, it gives me this answer

Array
(
    [0] => stdClass Object
        (
            [Tables_in_plugin (pro_hist%)] => pro_hist1579
        )

    [1] => stdClass Object
        (
            [Tables_in_plugin (pro_hist%)] => pro_hist1580
        )
)

but how can I echo this out?

if I do print_r($tables[0]) I get this result

stdClass Object
(
    [Tables_in_plugin (pro_hist%)] => pro_hist1579
)

Now I wanted to echo "pro_hist1579" ?

Share Improve this question edited Jan 26, 2022 at 19:57 tiago calado asked Dec 16, 2021 at 19:31 tiago caladotiago calado 6324 silver badges17 bronze badges 2
  • Just iterate over the array with foreach. That's not a WordPress, is it? – fuxia Commented Dec 16, 2021 at 19:36
  • yes, it is wp, $wpdb, the thing is that [Tables_in_plugin (pro_hist%)] , I dont seem to be able to reach it – tiago calado Commented Dec 16, 2021 at 19:39
Add a comment  | 

1 Answer 1

Reset to default 3

You could try changing the output type of get_results() to ARRAY_A or ARRAY_N and see what kind of array they give you.

E.g.

$tables = $wpdb->get_results("show tables like 'pro_hist%'", ARRAY_A);
var_dump($tables);
foreach( $tables as $table ) {
    var_dump($table);
}

If $table is an array and it has the name as the first value, then get the it for example with array_shift().

From the Code Reference,

wpdb::get_results( string $query = null, string $output = OBJECT )

$output

(string) (Optional) Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object ( ->column = value ), respectively. With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded.

Default value: OBJECT

发布评论

评论列表(0)

  1. 暂无评论