Create new table with Wordpress API
I'm trying to create a new plugin to track popular posts based on views and I have everything done and ready to go, but I can't seem to create a new table using the Wordpress API (I can do it with standard PHP or with phpMyAdmin, but I want this plugin to be self-sufficient).
I've tried several ways ($wpdb->query, $wpdb->get_results, dbDelta) but none of them will create the new table.
function create_table(){
global $wpdb;
$tablename = $wpdb->prefix.'popular_by_views';
$ppbv_table = $wpdb->get_results("SHOW TABLES LIKE '".$tablename."'" , ARRAY_N);
if(is_null($ppbv_table)){
$create_table_sql = "CREATE TABLE '".$tablename."' (
'id' BIGINT(50) NOT NULL AUTO_INCREMENT,
'url' VARCHAR(255) NOT NULL,
'views' BIGINT(50) NOT N开发者_JAVA百科ULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
);";
$wpdb->show_errors();
$wpdb->flush();
if(is_null($wpdb->get_results("SHOW TABLES LIKE '".$tablename."'" , ARRAY_N))) echo 'crap, the SQL failed.';
}
else echo 'table already exists, nothing left to do.';}
The problem was with the SQL statement, instead of
PRIMARY KEY ('id'),
UNIQUE ('id')
use
PRIMARY KEY (id),
UNIQUE (id)
.
精彩评论