Support » Plugin: WP Statistics » Fix for the db error when re-activating

  • Resolved kitchin


    This plugin uses dbDeltas incorrectly. The problem can be seen if you de-activate and then activate the plugin. Due to errors, you will not be able to activate the plugin. (This may require WP_DEBUG.)

    In wps-install.php the dbDelta strings should look like this:

    $create_useronline_table = ("CREATE TABLE {$wp_prefix}statistics_useronline (
    			ip varchar(20) NOT NULL,
    			timestamp int(10) NOT NULL,
    			date datetime NOT NULL,
    			referred text CHARACTER SET utf8 NOT NULL,
    			agent varchar(255) NOT NULL,
    			platform varchar(255),
    			version varchar(255),
    			PRIMARY KEY  (ID)
    		) CHARSET=utf8");

    The two fixes are:
    #1. Remove all bactick characters.
    #2. Put two spaces between “PRIMARY KEY” and “(ID)”

    It is unfortunate but true that WP dbDelta cannot handle backticks. The preg_match() in line 1669 (WP 3.9.2) of wp-admin/includes/upgrade.php will fail:
    preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i",...
    and the following line
    $fieldtype = $matches[1];
    will emit a warning if you turn on WP_DEBUG.

    The failure to match causes WP to try to recreate the table columns. That is usually OK, but not what you want.

    Problem #2 is worse, because MySQL will try to add an index that already exists. It is unfortunate but true that you need two spaces in there. MySQL does not see a difference, but WP does, due to the array_search() at line 1745.

    The other KEY lines should *not* have the double space, only the PRIMARY KEY. So problem #1 affects 5 strings; problem #2 is in 4 strings.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Fix for the db error when re-activating’ is closed to new replies.