$wpdb is null even after 'global $wpdb

2019-02-26 08:55发布

I have seen almost all links but still I am unable to solve my problem.

I am getting $wpdb as null.

I am checking it like this. I am doing this in single.php file

echo "<pre>";print_r($wpdb);"</pre>";

I have checked about following files. That all are loaded.

  1. wp-config.php
  2. wp-load.php
  3. wp-includes/wp-db.php

Please help me.

EDIT

I want to execute custom query like this.

$entries = $wpdb->get_results( $wpdb->prepare( "SELECT forms.form_title, entries. *
        FROM wp_visual_form_builder_forms AS forms
        INNER JOIN wp_visual_form_builder_entries AS entries ON   entries.form_id = forms.form_id" ) );

this is not working.

标签: wordpress
1条回答
倾城 Initia
2楼-- · 2019-02-26 09:44

If you read the documentation, you will notice this passage:

Always use the global $wpdb variable. (Remember to globalize $wpdb before using it in any custom functions.)

It's not very clear but I think that means you cannot use $wpdb outside of a function. What I recommend you do is create a function in your theme's functions.php file, and call that function from the single.php file.

查看更多
登录 后发表回答