2012-03-31 64 views
3

我需要在wordpress中爲插件創建一個自定義表格。我遵循幾個在線文本並創建了表格,但發現無法訪問。當試圖從表中選擇所有內容時,將不會返回任何內容,並且當嘗試使用數據庫瀏覽器插件查看錶時,我收到此錯誤:「您的SQL語法中有錯誤;請檢查與您的MySQL服務器相對應的手冊版本的正確語法,以響應插件的查詢(「SELECT SQL_CALC_FOUND_ROWS FROM wp-typeEvents LIMIT 0,100 ;;」)在'FROM wp-typeEvents LIMIT 0,100'at line 1'附近使用。使用dbDelta函數在Wordpress中創建表格

總之,我試圖用dbDelta來創建一個表。該表已創建,但存在某些問題,導致無法添加行或讀取其內容。

我讀過dbDelta可以finnicky功能,所以我試圖堅持自己的三條黃金法則:

在新行
-Putting主鍵之間的兩個空格-Putting每個字段其定義
-Having至少一個關鍵

下面的代碼:

global $wpdb; 

$tablename = "wp-typeEvents"; 
$query = "CREATE TABLE `" . $tablename . "` (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT, 
    `eventName` varchar(60) NOT NULL, 
    `location` varchar(60) DEFAULT '' NULL, 
    `price` double NOT NULL, 
    `description` text NOT NULL, 
    `paypal` varchar(60) NOT NULL, 
    PRIMARY KEY (`id`) 
    );"; 

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

任何想法?

+0

找出我自己的問題。該函數在表名中的連字符上窒息。 – Fabulinus 2012-03-31 19:23:56

回答

1

如果它是一個插件裏面,放在require_once()路徑是錯誤的。應該是:

require_once('/includes/upgrade.php'); 

應該更正爲能夠加載dbDelta()函數。

希望這會有所幫助。

+2

'require_once(ABSPATH。'wp-admin/includes/upgrade.php');'是正確的。 – ninty9notout 2012-11-29 12:54:35

+0

加載插件時,從** wp-admin **目錄中調用插件函數,因此不需要編寫整個路徑。 – 2012-11-29 15:04:14

+0

@ ninty9notout:現在我明白你的觀點:這是問題的道路。當我回答時,我不認爲這是事實,但我不記得了。無論如何感謝您的注意。 – 2012-11-29 17:43:34

3

不要硬編碼表名稱,使用$ wpdb->前綴(如其他人注意到的)。

請勿在字段名稱周圍使用引號。

還有其他有用的「規則」,以及,他們都在這裏列出: http://codex.wordpress.org/Creating_Tables_with_Plugins

Note that the dbDelta function is rather picky, however. For instance:

  • You must put each field on its own line in your SQL statement.
  • You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
  • You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
  • You must not use any apostrophes or backticks around field names.
  • Field types must be all lowercase.
  • SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
0

還拿注意到,當前dbDelta()功能無法解析排序關鍵字ASC |上唯一鍵DESC它將引用的列的名稱,它會將約束添加到剩餘的索引,因此它將嘗試添加觸發Unique Key錯誤的約束。看下面的例子:

嘗試創建這個questrong textry會觸發一個錯誤。

CREATE TABLE wp_test (
     id int(11) NOT NULL AUTO_INCREMENT, 
     email varchar(100) NOT NULL, 
     PRIMARY KEY (stcr_id), 
     UNIQUE KEY uk_email (subscriber_email ASC)) 
ENGINE = InnoDB 
DEFAULT CHARACTER SET utf8; 

默認情況下,排序類型是ASC,所以擺脫這一點將起作用。

CREATE TABLE wp_test (
     id int(11) NOT NULL AUTO_INCREMENT, 
     email varchar(100) NOT NULL, 
     PRIMARY KEY (stcr_id), 
     UNIQUE KEY uk_email (subscriber_email)) 
ENGINE = InnoDB 
DEFAULT CHARACTER SET utf8; 

我測試了這個WordPress的4.1.1

0

同時使用wordpress的dbDelta核心功能我也面臨着一些問題,並決定爲它創建一個功能:

/** 
* Prevents unnecessary re-creating index and repetitive altering table operations when using WordPress dbDelta function 
* 
* Usage Example: 
* 
* $table_name  = "ratings"; 
* 
* $table_columns = "id INT(6) UNSIGNED AUTO_INCREMENT, 
*     rate tinyint(1) NOT NULL, 
*     ticket_id bigint(20) NOT NULL, 
*     response_id bigint(20) NOT NULL, 
*     created_at TIMESTAMP"; 
* 
* $table_keys  = "PRIMARY KEY (id), 
*     KEY ratings_rate (rate), 
*     UNIQUE KEY ratings_response_id (response_id)"; 
* 
* create_table($table_name, $table_columns, $table_keys); 
* 
* Things that need to be considered when using dbDelta function : 
* 
* You must put each field on its own line in your SQL statement. 
* You must have two spaces between the words PRIMARY KEY and the definition of your primary key. 
* You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY. 
* You must not use any apostrophes or backticks around field names. 
* Field types must be all lowercase. 
* SQL keywords, like CREATE TABLE and UPDATE, must be uppercase. 
* You must specify the length of all fields that accept a length parameter. int(11), for example. 
* 
* Further information can be found on here: 
* 
* http://codex.wordpress.org/Creating_Tables_with_Plugins 
* 
* @param $table_name 
* @param $table_columns 
* @param null $table_keys 
* @param null $charset_collate 
* @version 1.0.1 
* @author Ugur Mirza Zeyrek 
*/ 
function create_table($table_name, $table_columns, $table_keys = null, $db_prefix = true, $charset_collate = null) { 
    global $wpdb; 

    if($charset_collate == null) 
     $charset_collate = $wpdb->get_charset_collate(); 
    $table_name = ($db_prefix) ? $wpdb->prefix.$table_name : $table_name; 
    $table_columns = strtolower($table_columns); 

    if($table_keys) 
     $table_keys = ", $table_keys"; 

    $table_structure = "($table_columns $table_keys)"; 

    $search_array = array(); 
    $replace_array = array(); 

    $search_array[] = "`"; 
    $replace_array[] = ""; 

    $table_structure = str_replace($search_array,$replace_array,$table_structure); 

    $sql = "CREATE TABLE $table_name $table_structure $charset_collate;"; 

    // Rather than executing an SQL query directly, we'll use the dbDelta function in wp-admin/includes/upgrade.php (we'll have to load this file, as it is not loaded by default) 
    require_once (ABSPATH . 'wp-admin/includes/upgrade.php'); 

    // The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary 
    return dbDelta($sql); 
} 

https://github.com/mirzazeyrek/wordpress_create_table