douhuan1950 2011-08-24 07:12
浏览 48
已采纳

在非WordPress文件中的函数中调用$ wpdb

Playing around with the query SHOW TABLE STATUS in a non-WordPress core/plugin/template file using wp-load.php to access WordPress functions and database connection.

require_once('wp-load.php');

function mysqlInfo() {
    $info = $wpdb->get_results('SHOW TABLE STATUS');
    foreach($info as $table) {
        echo $table->name;
    }
}

I keep running into this error:

Fatal error: Call to a member function get_results() on a non-object in /../../../file.php

I have tried declaring $wpdb as a global variable and tried creating a new class out of $wpdb

  • 写回答

1条回答 默认 最新

  • dsfw2154 2011-08-24 16:49
    关注

    You have to declare the global variable within the function

    function mysqlInfo() {
    global $wpdb;
    $info = $wpdb->get_results('SHOW TABLE STATUS');
    foreach($info as $table) {
        echo $table->Name;
    }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 目前wxid加好友的渠道都被和谐了吗
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出