WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Creating tables with plugins doesn't work with 3.0 (6 posts)

  1. Canha
    Member
    Posted 3 years ago #

    I've been trying to create MySQL tables for a plugin in WordPress 3.0, but it isn't working. It's the same as described in the Codex (http://codex.wordpress.org/Creating_Tables_with_Plugins), but it isn't working. Incidentally, I've also noticed that the WP125 plugin - http://wordpress.org/extend/plugins/wp125/ - (which uses the same database table creating written in the Codex) has stopped working in 3.0.

    When a plugin is installed, it runs without an error but the table isn't created (doesn't show up in PhpMyAdmin). When information is inserted into the table thru the plugin, no errors appear (even with the non-existant table).

    Anyone know what's up?

  2. Canha
    Member
    Posted 3 years ago #

    By the way, the code I used:

    $jal_db_version = "1.0";
    
    function jal_install () {
       global $wpdb;
       global $jal_db_version;
    
       $table_name = $wpdb->prefix . "liveshoutbox";
       if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
    
          $sql = "CREATE TABLE " . $table_name . " (
    	  id mediumint(9) NOT NULL AUTO_INCREMENT,
    	  time bigint(11) DEFAULT '0' NOT NULL,
    	  name tinytext NOT NULL,
    	  text text NOT NULL,
    	  url VARCHAR(55) NOT NULL,
    	  UNIQUE KEY id (id)
    	);";
    
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          dbDelta($sql);
    
          add_option("jal_db_version", $jal_db_version);
    
       }
    }

    The error it returns:

    The plugin generated 296 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

    It's a clean WP 3.0 install, no plugins activated.

  3. redwallhp
    Member
    Posted 3 years ago #

    I think this is they key here:

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);

    I think something with dbDelta() was either changed or removed.

  4. redwallhp
    Member
    Posted 3 years ago #

    Here's what I've found from a quick search:

    http://core.trac.wordpress.org/ticket/13715

    http://36flavours.com/2010/04/wp-dbdelta-function-cannot-modify-unique-keys/

    http://hakre.wordpress.com/2010/01/30/wpdb-include-problems-in-wordpress-3-0/

    I wonder if removing or commenting-out this line might help?

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    One of the posts seems to suggest that the require statements is re-instantiating a class and it's breaking things. If that's the case (I don't want to try the change tonight), maybe a if/else to check the WordPress version is in order?

  5. Canha
    Member
    Posted 3 years ago #

    After a lot of tinkering around, I've found the problem in the above code. Well, not as much "found" but more like "stumbled upon". Removing the "id" column, all seems to work well.

    The following code:

    $jal_db_version = "1.0";
    
    function jal_install () {
       global $wpdb;
       global $jal_db_version;
    
       $table_name = $wpdb->prefix . "liveshoutboxzz";
       if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
    
          $sql = "CREATE TABLE " . $table_name . " (
    	  time bigint(11) DEFAULT '0' NOT NULL,
    	  name tinytext NOT NULL,
    	  text text NOT NULL,
    	  url VARCHAR(55) NOT NULL
    	);";
    
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          dbDelta($sql);
    
          add_option("jal_db_version", $jal_db_version);
    
       }
    }
    register_activation_hook(__FILE__, 'jal_install');

    Seems to work fine, creating the table, returning no errors - which is odd, taking into account that the code I've mentioned earlier works when executed directly (in PhpMyAdmin, not in WordPress). Can't tell if this is a WP 3.0 bug.

    I've tried redwallhp's of commenting out this line:
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    But no success either.

    I'm baffled, because although in this case, when the plugin is activated and error occurs, the table is CREATED anyway. And the worst (or best) is that the plugin DESPITE SHOWING AN ERROR ON INSTALL, seems to work perfectly.

    Although my problem was solved, I'm leaving this as unsolved because it just doesn't make any sense. dbDelta() seems to be working fine (and no where it's included in the 3.0 changelog), yet something isn't right.

  6. nallu
    Member
    Posted 3 years ago #

    Hi,

    I had the same problem today and resolved it. dbDelta function is not working.
    Instead use $wpdb->query function.

    For eg. to create a table named mytable with 3 fields id, name and value with id as the primary key use the following code in the install function.

    $table_name = "mytable";
    $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
    id int(10) NOT NULL AUTO_INCREMENT,
    name VARCHAR(25) NOT NULL,
    value VARCHAR(45) NOT NULL,
    PRIMARY KEY (id)
    );" ;

    $wpdb->query($sql);

    Hope it helps.

    Thanks,
    nallu

Topic Closed

This topic has been closed to new replies.

About this Topic