WP Statistics
[resolved] Fix for the db error when re-activating (2 posts)

  1. kitchin
    Posted 1 year ago #

    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.


  2. Greg Ross
    Plugin Author

    Posted 1 year ago #

    It looks like at some point there did fix the backtick issue (https://core.trac.wordpress.org/ticket/8014) but the docs still say not to use them so I'll update it.


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Statistics
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic