Matt
Matt

Reputation: 1

Wordpress Database Fatal Error

I'm receiving this error when trying to run a PHP script against my WordPress database:

Fatal error: Cannot redeclare class wpdb

Can anyone help me resolve this?


Edit

Code:

require_once(dirname(__FILE__) . '/../../wp-config.php'); 
require_once(dirname(__FILE__) . '/../../wp-includes/wp-db.php'); 
$wpdb->show_errors(); 
$dupes = $wpdb->get_results('select bad_rows.* from wp_posts as bad_rows inner join ( select post_title, MIN(id) as min_id from wp_posts group by post_title having count(*) > 1 ) as good_rows on good_rows.post_title = bad_rows.post_title and good_rows.min_id <> bad_rows.id; '); 

foreach ($dupes as $dupe) 
{ 
  echo $dupe->post_title ."\n"; 
}

$wpdb->query(' delete bad_rows.* from wp_posts as bad_rows inner join ( select post_title, MIN(id) as min_id from wp_posts group by post_title having count(*) > 1 ) as good_rows on good_rows.post_title = bad_rows.post_title and good_rows.min_id <> bad_rows.id; ');

Upvotes: 0

Views: 2453

Answers (4)

Syed Ali
Syed Ali

Reputation: 161

check your file is calling properly then make $wpdb variable as a global variable(global $wpdb;)

 include_once( __DIR__. '/../../../../wp-config.php');
    include_once( __DIR__. '/../../../../wp-load.php');
    include_once( __DIR__. '/../../../../wp-includes/wp-db.php');
    class Myclass{
     function anyname(){
        global $wpdb;
        $results = $wpdb->get_results( "SELECT * FROM table", OBJECT );
        print_r($results) ;
     }

Upvotes: 0

wordpress dude
wordpress dude

Reputation: 11

I replaced the corrupted functions.php file in the wp-includes folder with a new functions.php file from a new install of wordpress. Make sure you get one from the same version of wordpress. Not sure if a different version will get the same results.

This fixed my issue of the Call to undefined function require_wp_db() /filename/filename/...wp-settings.php on line 71, and I was able to login.

I suggest to back up your functions.php file before installing anything like sidebars. That's what caused my problem.

Upvotes: 1

Mathew
Mathew

Reputation: 8279

I can only go by my own Wordpress install, but;

wp-config.php;

[line 30] require_once(ABSPATH . 'wp-settings.php');

wp-settings.php;

[line 242] require_wp_db();

wp-includes/functions.php

[line 2534] function require_wp_db() {
                global $wpdb;
                if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
                    require_once( WP_CONTENT_DIR . '/db.php' );
                else
                    require_once( ABSPATH . WPINC . '/wp-db.php' );
            }

Although you (and the require_wp_db function) use require_once, it seems as though your call to wp-includes/wp-db.php is unnecessary. Try commenting it out and see if the problem disappears. If not, follow the trail down into wp-config.php, commenting out includes as you go. Hopefully you'll be able to isolate the line that is causing the issue...

Upvotes: 0

Pekka
Pekka

Reputation: 449425

You seem to be including the Wordpress bootstrap twice. Eliminate the second call or use require_once().

How does your PHP script look like?

Upvotes: 0

Related Questions