title: How to include $wpdb in wordpress plugin?
body: <p>I've been developing for some time on a <code>plugin</code> in <code>wordpress</code>, but one problem keeps bugging me. I want to export a database-table as an excel file and therefor i need access to the global <code>$wpdb-&gt;variable</code> from a file in my plugin directory.</p>
  
  <p>I found a blog entry that explains what classes i should include, but this doesn't work (link is below). As you can see, i do a <code>var_dump</code>, but it never reaches that point. If i leave the includes of <code>wp-config</code> and <code>wp-load</code> out of the code, the dump <code>returns NULL</code>, so i'm guessing there is a problem with the imports.</p>
  
  <p>Anyway, i was hoping someone could help me with this problem. I don't necessarily need a fix for my approach, I just need a way to export an array of data (fetched from my db) to excel in wordpress. Any help would be appreciated. Thanks in advance.</p>
  
  <p><a href="http://www.notesbit.com/index.php/web-mysql/web-scripts/standalone-access-the-wordpress-database-using-wpdb/" rel="noreferrer">http://www.notesbit.com/index.php/web-mysql/web-scripts/standalone-access-the-wordpress-database-using-wpdb/</a></p>
  
  <pre><code>include_once('../../../wp-config.php');
  include_once('../../../wp-load.php');
  include_once('../../../wp-includes/wp-db.php');
  
  var_dump($wpdb);
  $filter = get_where_clause();
  $order = get_order_by_clause();
  
  $data = $wpdb-&gt;get_results("SELECT * FROM " . $table_prefix . "team_data" . $filter . $order, ARRAY_A);
  
  $result = array();
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  I cannot include the <code>wp-config</code>, it gives constant errors. I know where the bug is taking place, I just need to find a work-around. when looking at the <code>wp-settings</code> page (which is included by the wp-config) you will find this line of code:</p>
  
  <pre><code>foreach ( wp_get_active_and_valid_plugins() as $plugin )
      include_once( $plugin );
  unset( $plugin );
  </code></pre>
  
  <p>this is where there is a bug. I just don't know how i should work around this bug.</p>
  
  <p><strong>EDIT 2:</strong>
  Problem solved. When including the file, i included the <code>wp-config</code> more than once (even though i stated it should only be included one time). I solved the problem by using the following code.</p>
  
  <pre><code>global $wpdb, $table_prefix;
  
  if(!isset($wpdb))
  {
      require_once('../../../../wp-config.php');
      require_once('../../../../wp-includes/wp-db.php');
  }
  </code></pre>