向Wordpress数据库添加新列 [英] Add new column to wordpress database
问题描述
我尝试更新插件.因此,我必须升级mysql_table.但是,当尝试新列时,程序会获得异常.
I trying update my plugin. So I must upgrade mysql_table. But when trying new column, program get exception.
这是我当前的表格:
$sql = "CREATE TABLE {$table_name} (
say_id int(11) not null AUTO_INCREMENT,
customer_mail text not null,
customer_name text not null,
customer_messagge text not null,
messagge_date_time datetime not null,
PRIMARY KEY (say_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta($sql);
现在我将向colum添加更多一张桌子.我尝试使用Alter表,这种方法可以工作一次并添加一列,但是再刷新一次,我会收到此错误.
Now I am add colum more one table. I try Alter table, this working one time and add a column but one more refresh I get this error.
这是我的代码
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
这是我的错误
WordPress数据库错误:[重复的列名'say_state'] ALTER TABLE wp_customer_say ADD say_state INT(1)NOT NULL DEFAULT 1
WordPress database error: [Duplicate column name 'say_state'] ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1
我看到此错误,然后尝试尝试;
I see this error and ı try this;
$query = $wpdb->query("select * from wp_customer_say");
$respond = mysql_num_fields( $query );
$column_array = array();
for($i = 0; $i < $respond ; $i++):
$column_array[] = mysql_field_name($query,$i);
endfor;
if( !in_array("say_state",$column_array) ):
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
endif;
我得到这个错误.
Warning: mysql_num_fields() expects parameter 1 to be resource, integer given in
请帮助.谢谢你. 对不起,英语不好.
Help please. Thank you. Sorry bad english.
推荐答案
使用此查询.我只使用mysql-standred来通过快速查询获取字段名称,这将解决您的问题:
Use this query. I use only mysql-standred for get field name by fast query and this will solve your problem:
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'wp_customer_say' AND column_name = 'say_state'" );
if(empty($row)){
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
}
这篇关于向Wordpress数据库添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!