views:

333

answers:

3

I'm receiving this error when trying to run a php script against my wordpress DB:

Fatal error: Cannot redeclare class wpdb

Can anyone help me to 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; ');
A: 

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

How does your PHP script look like?

Pekka
<?phprequire_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_rowsinner join (select post_title, MIN(id) as min_idfrom wp_postsgroup by post_titlehaving count(*) > 1) as good_rows on good_rows.post_title = bad_rows.post_titleand good_rows.min_id <> bad_rows.id;'); foreach ($dupes as $dupe) {echo $dupe->post_title ."\n";}
Matt
$wpdb->query('delete bad_rows.*from wp_posts as bad_rowsinner join (select post_title, MIN(id) as min_idfrom wp_postsgroup by post_titlehaving count(*) > 1) as good_rows on good_rows.post_title = bad_rows.post_titleand good_rows.min_id <> bad_rows.id;');?>
Matt
A: 

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...

MatW
A: 

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.

wordpress dude