Forums

[resolved] WP 3.1 - pg4wp - Cannot redeclare class wpdb (17 posts)

  1. touisk
    Member
    Posted 1 year ago #

    Hi,

    My blog was ok whith WordPress 3.0.4 and pg4wp 1.1.0 until I updated to wordpress 3.1 !

    PHP logs shows a PHP Fatal error:  Cannot redeclare class wpdb in /usr/share/wordpress/htdocs/wp-content/db.php(37) : eval()'d code on line 52

    In wordpress 3.0.5 the require_wp_db(); - call in the /wp-settings.php - is implemented in the file /wp-includes/functions.php which is :

    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' );
    }

    The class wpdb - in /wp-includes/wp-db.php - is evaluated by the eval function in /wp-content/db.php file from pg4wp :

    eval( str_replace( array_keys($replaces), array_values($replaces), file_get_contents(ABSPATH.'/wp-includes/wp-db.php')));

    The evaluation is done after the replacement of MySQL names to postgres ones.

    But in WordPress 3.1, require_wp_db() function has moved into the file /wp-includes/load.php. The new implementation is :

    function require_wp_db() {
            global $wpdb;
    
            require_once( ABSPATH . WPINC . '/wp-db.php' );
            if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
                    require_once( WP_CONTENT_DIR . '/db.php' );
    
            if ( isset( $wpdb ) )
                    return;
    
            $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
    }

    in those conditions; /wp-includes/wp-db.php is evaluated twice ! with the wp-content/db.php. The class wpdb can not be redeclared twice.

    My solution is to ... come back. I simply re-implemented the WP 3.1 require_wp_db() function like this :

    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' );
    
            if ( isset( $wpdb ) )
                    return;
    
            $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
    }

    Hope, this could help ...

  2. davorj
    Member
    Posted 1 year ago #

    Thank you for your fine post touisk. You saved me lots of time.

    That illogical piece of code should have never made it into production! I hope it will be fixed in the next release.

  3. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    "Cannot redeclare class" suggests that the upgrade did not complete successfully. The answer is to not edit the core code but:

    - switc to the Twenty Ten theme by renaming your current theme's folder in wp-content/themes using FTP or whatever file management application your host provides.

    - reset the plugins folder by FTP or phpMyAdmin.

    - re-upload all files & folders - except the wp-content folder - from a fresh download of WordPress.

    - re-run the upgrade manually using wp-admin/upgrade.php.

  4. bimki
    Member
    Posted 1 year ago #

    After Your advice I have following error - it occures when I try to acces /my-wordpress/wp-admin page...

    Warning: pg_query() [function.pg-query]: No PostgreSQL link opened yet in /home/wordpress/www/wp-content/plugins/pg4wp/driver_pgsql.php on line 318
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/wordpress/www/wp-content/plugins/pg4wp/driver_pgsql.php:318) in /home/wordpress/www/wp-includes/pluggable.php on line 897

    help :)

  5. bimki
    Member
    Posted 1 year ago #

    I still have this warning:
    "Warning: pg_query() [function.pg-query]: No PostgreSQL link opened yet in /home/wordpress/www/wp-content/plugins/pg4wp/driver_pgsql.php on line 318"
    and I don't know what to do...
    accessing wp-admin is impossible. Do You have any advice?

  6. davorj
    Member
    Posted 1 year ago #

    @bimki: do as touisk or esmi explained and all should work fine.

    @esmi: is it safe to update to 3.1.1 automatically this time (regardless of whether one has followed touisk solution and changed to core files, or your manual update)? I don't know what caused the update to fail last time, but judging from the posts concerning this, it isn't isolated to this thread... and it wouldn’t be nice if something similar happened again.

  7. bimki
    Member
    Posted 1 year ago #

    @davorj - I've done it as touisk explained (I've changed implementation of require_wp_db function)...

  8. davorj
    Member
    Posted 1 year ago #

    @bimki: Then I don't know, sorry...

    @esmi: WordPress 3.1.1 still doesn't auto-update well... I don't think it has anything to do with the auto-update. When I compare my WordPress files with the original files after I auto-update, all are the same, except the ones in wp-content. So I don't think it has anything to do with auto-update failure. The original sourcecode touisk mentioned for require_wp_db() is not logical or at least badly coded.

    I really think this should be looked into.

  9. jamie3000
    Member
    Posted 10 months ago #

    I seem to get the same error as bimki.
    I have a fresh installation of wp 3.2.1.
    I ran the installer, and it seems okay.
    Then I tried to add a user, and I get that error, on line 318 of the driver.

    Turning on debugging, I see the following: two error messages:

    Error running :
    SELECT SQL_CALC_FOUND_ROWS wp_users.ID FROM wp_users WHERE 1=1 ORDER BY user_login ASC LIMIT 20
    ---- converted to ----
    SELECT DISTINCT wp_users."ID" FROM wp_users WHERE 1=1 ORDER BY user_login ASC LIMIT 20
    ----
    ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
    LINE 1: ...CT wp_users."ID" FROM wp_users WHERE 1=1 ORDER BY user_login...
                                                                 ^
    ---------------------
    Error running :
    SELECT COUNT(NULLIF(<code>meta_value</code> LIKE '%administrator%', FALSE)), COUNT(NULLIF(<code>meta_value</code> LIKE '%editor%', FALSE)), COUNT(NULLIF(<code>meta_value</code> LIKE '%a
    uthor%', FALSE)), COUNT(NULLIF(<code>meta_value</code> LIKE '%contributor%', FALSE)), COUNT(NULLIF(<code>meta_value</code> LIKE '%subscriber%', FALSE)), COUNT(*) FROM wp_user
    meta WHERE meta_key = 'wp_capabilities'
    ---- converted to ----
    SELECT COUNT(NULLCASE WHEN meta_value ILIKE '%administrator%' THEN  FALSE)) ELSE  COUNT(NULLIF(meta_value ILIKE '%editor%', FALSE END), COUNT(NULLCASE W
    HEN meta_value ILIKE '%author%' THEN  FALSE)) ELSE  COUNT(NULLIF(meta_value ILIKE '%contributor%', FALSE END), COUNT(NULLCASE WHEN meta_value ILIKE '%su
    bscriber%' THEN  FALSE)) ELSE  COUNT(* END FROM wp_usermeta WHERE meta_key = 'wp_capabilities'
    ----
    ERROR:  syntax error at or near "WHEN"
    LINE 1: SELECT COUNT(NULLCASE WHEN meta_value ILIKE '%administrator%...

    For the first error, I am guessing the ORDER BY could be ignored completely... it's just counting, right?

  10. jamie3000
    Member
    Posted 10 months ago #

    I have fixed both of my problems...

    1. I just removed the 'DISTINCT' keyword from the substitution. The MySQL version doesn't have DISTINCT, so I guess this is okay.

    2. The 'IF' handling needed to NOT translate if it is a NULLIF, as postgres also supports NULLIF.

  11. Bob110591
    Member
    Posted 10 months ago #

    Hi all,
    I am using WordPress 3.2.1
    There seems to be an issue with Pg4Wp plugin. I've followed all the instructions. But still I am getting not properly configured website error in the browser.
    Should there be any code changes to be made?
    All the files are in the correct places where they have to be.
    What could be the possible problem?

    I am a beginner (for wordpress and postgreSQL)

    Any help would be appreciated

  12. davorj
    Member
    Posted 10 months ago #

    Yes, require_wp_db() should be adapted as stated in the first post. Follow it and it should work. In postgres, a database should already be available and a user should have all rights on it.

    Regards,
    Davor

  13. Bob110591
    Member
    Posted 10 months ago #

    Thank you.

    Now it says
    "Call to undefined function pg_query() in "wordpress/wp-content/plugins/pg4wp/driver_pgsql.php on line 318"

  14. davorj
    Member
    Posted 10 months ago #

    You have the pg module in PHP enabled? Search the net. Check also with phpinfo() that your pg_drivers are loaded. Check the php logs, etc.

    I do know that if you follow the installation manuals and pg4wp installation, all should work.

  15. Bob110591
    Member
    Posted 10 months ago #

    Thanks davorj. Resolved this.

    Now it gives me 2 warnings, 5 notices and 1 fatal error:
    Warnings: No PostgreSQL link opened yet in driver_pgsql.php in line 318 and 319

    Notices:Constant EZSQL_VERSION,object,OBJECT_K,ARRAY_A,ARRAY_N already defined in wp-includes/wp-db.php

    Fatal Error:Cannot redeclare class wpdb in wp-includes/wp-db.php

    Though the error is similar to the one mentioned in the first post, the solution mentioned is not working.

  16. Bob110591
    Member
    Posted 10 months ago #

    Hi,
    I've cleared all others except the fatal error.

    I tried changing the functions and caller functions of require_wp_db().
    No change in the error function.

    I am guessing that because the dp.php file(using replace) is replacing the term mysql with pgsql and reading the class "wpdp". The function require_wp_db() is also declaring the class wpdp. Thus class wpdp is being declared 2 times which is what I think is causing this problem.
    Am I right?
    Is there any way out?
    I am desperately in need for a solution.

    Thanks in advance

  17. hawk__
    Member
    Posted 9 months ago #

    Hello

    I'm currently updating PG4WP make it fully compatible with WordPress up to version 3.2.1

    I've released a beta that works ok on my setup, if you want to give it a try you'll find a direct link on http://www.hawkix.net.

    If upgrading PG4WP from any earlier version, don't forget to replace the db.php file with the one from the pg4wp/ directory.

    Beta 1.2.0b1 should at least have all the issues found in this topic resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic