我正在尝试使用我的Wordpress插件来创建包含激活数据的表。
SQL似乎已被执行并且本身是正确的(事实上,如果我手动将其复制到SQL服务器,它就可以工作)
我的PHP代码如下
$res
如果我输出Array ( [orhub_ajax_preview_galleries] => Created table orhub_ajax_preview_galleries )
,我会得到:$sql
这表明一切都很好。但是,如果我检查数据库,则该表不存在,并且该插件无法存储数据。
正如我所说,我尝试输出maybe_create_table
并将其直接粘贴到phpMyAdmin中。这很有用,所以问题似乎不在查询中。
那么还有什么可能是错的?
顺便说一句,我也试过了public function setMetaDataForObject(string bucket, string objectKey, struct metadata){
var dateTimeString = GetHTTPTimeString(now());
writedump(metadata);
var cs = "PUT\n\nimg/png\n#dateTimeString#\nx-amz-copy-source:/#arguments.bucket#/#arguments.objectKey#\nx-amz-metadata-directive:REPLACE\n";
for(key in arguments.metadata){
cs &= "#key#:#metadata[key]#\n";
}
cs &= "/#arguments.bucket#/#arguments.objectKey#";
writeoutput(cs);
var signature = createSignature(cs);
var httpReq = new http();
httpReq.setMethod("PUT");
httpReq.setUrl("http://#arguments.bucket#.s3.amazonaws.com/#arguments.objectKey#" );
//httpReq.setTimeout(timeout="300");
httpReq.addParam(type="header", name="Date", value="#dateTimeString#");
httpReq.addParam(type="header", name="Content-Type", value="img/png");
httpReq.addParam(type="header", name="x-amz-copy-source", value="/#arguments.bucket#/#arguments.objectKey#");
httpReq.addParam(type="header", name="x-amz-metadata-directive", value="REPLACE");
httpReq.addParam(type="header", name="Authorization", value="AWS #variables.accessKeyId#:#signature#");
for(var i in arguments.metadata){
httpReq.addParam(type="header", name="#i#", value="#arguments.metadata[i]#");
}
//httpReq.addParam(type="body", value="#fileS3#");
var result = httpReq.send().getPrefix(); writedump(result);
}
,但那也没有用到
答案 0 :(得分:1)
在dbDelta之前添加它:
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
您必须添加该行以显式加载运行dbDelta所需的核心部分。
答案 1 :(得分:1)
好的,您的CREATE TABLE
栏中gallery_select_by
的评论中包含非法字符
尝试:
global $wpdb;
$table_name = $wpdb->prefix . "ajax_preview_galleries";
$charset_collate = $wpdb->get_charset_collate();
//Table definition
$sql = "CREATE TABLE $table_name (
gallery_id int(10) unsigned NOT NULL AUTO_INCREMENT,
gallery_name varchar(100) COLLATE utf8_unicode_ci NOT NULL,
gallery_slug varchar(100) COLLATE utf8_unicode_ci NOT NULL,
gallery_selected_terms text COLLATE utf8_unicode_ci NOT NULL,
gallery_select_by tinyint(3) unsigned NOT NULL COMMENT '0 - categories only. 1 - tags only. 2 - both',
gallery_post_count tinyint(3) unsigned NOT NULL,
gallery_custom_class_container varchar(200) COLLATE utf8_unicode_ci NOT NULL,
gallery_custom_class_buttons varchar(200) COLLATE utf8_unicode_ci NOT NULL,
gallery_transition_time int(10) unsigned NOT NULL DEFAULT '500',
gallery_loading_type tinyint(3) unsigned NOT NULL DEFAULT '1',
gallery_navigator_loop tinyint(3) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (gallery_id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$res = dbDelta($sql);
问题是,你的评论中有:
和;
,;
可能是发出sql语句的信号,所以你有错误。
我尝试搜索转义它,但只找到this表示字符串文字,没有关于冒号和分号的内容。
希望这有帮助。
答案 2 :(得分:0)
搞定了!
看起来,问题是其中一个专栏的评论。事实上,我怀疑同样多,我已经尝试删除评论,尽管gallery_select_by column
上的评论已经引起了我的注意。感谢dingo_d将我的注意力指向那条线!
WordPress Codex确实指定“dbDelta函数相当挑剔。例如:
看起来,这些仅仅是一些例子,dbDelta()甚至可以抱怨评论。我无法找到dbDelta的“规则”的完整列表,但至少我得到了我的工作。
顺便说一下:如前所述,我曾经在未创建表时从dbDelta()获取此结果
Result: Array ( [orhub_ajax_preview_galleries] => Created table orhub_ajax_preview_galleries )
现在该插件正在运行,相反,我得到一个空数组。去搞清楚! 这对我来说似乎很特殊,因为它完全违反直觉(作为dbDelta()的其他方面),因此我可能很高兴知道,并且我指出了可能与同样问题作斗争的其他人。
似乎在处理dbDelta()时,必须考虑到“特殊规则”适用并且在其他地方工作的查询可能在这里不起作用(事实上,正如我所提到的,我原来的sql工作了例如直接放在phpMyAdmin中)。这个功能的结果可能不是那么好......
答案 3 :(得分:0)
您可能需要尝试此function:
$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);
/**
* 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);
}