在function.php中的wordpress数据库中创建一个表

时间:2015-12-20 10:56:36

标签: php sql wordpress

我试图在我的wordpress数据库中创建一个自定义表以保存剧集率,我想使用PHP函数,因为我不想创建一个插件 我使用这个代码,但它不起作用,表不会被创建,任何帮助。

function ratings_fansub_create() {

    global $wpdb;
    $table_name = $wpdb->prefix. "ratings_fansub";
    global $charset_collate;
    $charset_collate = $wpdb->get_charset_collate();
    global $db_version;

    if( $wpdb->get_var("SHOW TABLE LIKE " . $table_name)!= $table_name)
    {   $create_sql = "CREATE TABLE ".$table_name. "(
            ratings_fansub_id INT(11) NOT NULL auto_increment,
            ratings_fansub_postid INT(11) NOT NULL ,
            ratings_fansub_posttitle TEXT NOT NULL,
            ratings_fansub_rating INT(2) NOT NULL ,
            ratings_fansub_timestamp VARCHAR(15) NOT NULL ,
            ratings_fansub_ip VARCHAR(40) NOT NULL ,
            ratings_fansub_host VARCHAR(200) NOT NULL,
            ratings_fansub_username VARCHAR(50) NOT NULL,
            ratings_fansub_userid int(10) NOT NULL default '0',
            ratings_fansub_PRIMARY KEY (id))$charset_collate;";
    }
                require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta( $create_sql );




    //register the new table with the wpdb object
    if (!isset($wpdb->ratings_fansub)) 
    {
        $wpdb->ratings_fansub = $table_name; 
        //add the shortcut so you can use $wpdb->stats
        $wpdb->tables[] = str_replace($wpdb->prefix, '', $table_name); 
    }

}
add_action( 'init', 'ratings_fansub_create');

1 个答案:

答案 0 :(得分:2)

你的两个SQL语句有点错误。所以

  • "SHOW TABLE LIKE " . $table_name已替换为"SHOW TABLES LIKE '" . $table_name . "'"
  • ratings_fansub_PRIMARY KEY (id)已替换为PRIMARY KEY (ratings_fansub_id)

以下修改后的代码:

function ratings_fansub_create() {

    global $wpdb;
    $table_name = $wpdb->prefix. "ratings_fansub";
    global $charset_collate;
    $charset_collate = $wpdb->get_charset_collate();
    global $db_version;

    if( $wpdb->get_var("SHOW TABLES LIKE '" . $table_name . "'") !=  $table_name)
    {   $create_sql = "CREATE TABLE " . $table_name . " (
            ratings_fansub_id INT(11) NOT NULL auto_increment,
            ratings_fansub_postid INT(11) NOT NULL ,
            ratings_fansub_posttitle TEXT NOT NULL,
            ratings_fansub_rating INT(2) NOT NULL,
            ratings_fansub_timestamp VARCHAR(15) NOT NULL,
            ratings_fansub_ip VARCHAR(40) NOT NULL ,
            ratings_fansub_host VARCHAR(200) NOT NULL,
            ratings_fansub_username VARCHAR(50) NOT NULL,
            ratings_fansub_userid int(10) NOT NULL default '0',
            PRIMARY KEY (ratings_fansub_id))$charset_collate;";
    }
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta( $create_sql );




    //register the new table with the wpdb object
    if (!isset($wpdb->ratings_fansub))
    {
        $wpdb->ratings_fansub = $table_name;
        //add the shortcut so you can use $wpdb->stats
        $wpdb->tables[] = str_replace($wpdb->prefix, '', $table_name);
    }

}
add_action( 'init', 'ratings_fansub_create');

在旁注中,您不需要使用'ratings_fansub_'为每个列名添加前缀。没有什么可以阻止你使用像

这样的创建语句
"CREATE TABLE " . $table_name . " (
            id INT(11) NOT NULL auto_increment,
            postid INT(11) NOT NULL ,
            posttitle TEXT NOT NULL,
            rating INT(2) NOT NULL,
            timestamp VARCHAR(15) NOT NULL,
            ip VARCHAR(40) NOT NULL ,
            host VARCHAR(200) NOT NULL,
            username VARCHAR(50) NOT NULL,
            userid int(10) NOT NULL default '0',
            PRIMARY KEY (id))$charset_collate;";

希望有所帮助。