简体   繁体   English

在Wordpress中激活插件后未创建数据库表

[英]Database table does not create upon plugin activation in wordpress

I am trying to create a database table within a my wordpress plugin main file. 我正在尝试在我的wordpress插件主文件中创建数据库表。 Here is my code to create the database table. 这是我创建数据库表的代码。 When I activate the plugin from Wordpress control panel, plugin activate but database tables doesn't create. 当我从Wordpress控制面板激活插件时,插件会激活,但不会创建数据库表。

     <?php   
     function keywords_ranker_install() {
 global $wpdb;
 global $keyword_rankerdb;
 $keyword_rankerdb = "1.0";
 $table_name = $wpdb->prefix . "search_engine";

        if($wpdb->get_var("SHOW TABLES LIKE '$table_name'")!= $table_name) {

        $sql = "CREATE TABLE IF NOT EXISTS ".$table_name." (
        id int(11) NOT NULL AUTO_INCREMENT,
        engine text NOT NULL,
        PRIMARY KEY ('id'));";

    //reference to upgrade.php file
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($sql);
    //$wpdb->query($sql);

    //update_option('keyword_ranker_version',$keyword_rankerdb);
    }
  //action hook for plugin activation 

   }//end of plugin installation
      register_activation_hook(__FILE__,'keywords_ranker_install' );

?> ?>

I'm not sure what I'm missing, and any help would be much appreciated! 我不确定我缺少什么,任何帮助将不胜感激!

Your syntax is wrong and this IF NOT EXISTS is not needed because your condition is already checking that. 您的语法错误,并且不需要IF NOT EXISTS,因为您的条件已经在检查该情况。

NOTE: I switched it to all single quotes so its easier to follow and removed the single quotes in (id); 注意:我将其切换为所有单引号,因此更易于遵循,并删除了(id)中的单引号;

function keywords_ranker_install() {
global $wpdb;
global $keyword_rankerdb;
$keyword_rankerdb = "1.0";
$table_name       = $wpdb->prefix . "search_engine";

if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {

    $sql = 'CREATE TABLE ' . $table_name . ' (
    id int(11) NOT NULL AUTO_INCREMENT,
    engine text NOT NULL,
    PRIMARY KEY (id))';

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

    //update_option('keyword_ranker_version',$keyword_rankerdb);
}
//action hook for plugin activation

}

//end of plugin installation
register_activation_hook( __FILE__, 'keywords_ranker_install' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM