2014-01-08 38 views
1

我想在我的wordpress插件主文件中创建一个数据库表。这是我的代码来创建数据库表。当我从Wordpress控制面板激活插件时,插件会激活,但不会创建数据库表。数据库表不会在插件激活时在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'); 

?>

我不知道我错过了什么,任何帮助,将不胜感激!

回答

1

你的语法错了,这个IF NOT EXISTS不需要,因为你的条件已经在检查了。

注:我将它切换为所有单引号,以便更容易地遵循并删除(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'); 
+0

非常感谢您的帮助@topdown。我想我需要重写我的代码。 – user3173216

相关问题