admin管理员组文章数量:1130349
To manage database plugin update, I changed my code using dbDelta.
old code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
$is_data_inserted = $wpdb->query( $query_string );
if( !$is_data_inserted ){
$message_error = sprintf( "impossible to insert the data %s for the table %s!",
serialize( $data_item_attrs ),
$table_name
);
$wpdb->show_errors();
wp_error_log( $message_error, "Insert Data Error" );
return false;
}
return true;
}
new code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
dbDelta( $query_string );
}
My problem is using dbDelta disturbs inserts containing the character ";".
It´s because in dbDelta, there is ";" as delimiter.
How can I fix this ?
To manage database plugin update, I changed my code using dbDelta.
old code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
$is_data_inserted = $wpdb->query( $query_string );
if( !$is_data_inserted ){
$message_error = sprintf( "impossible to insert the data %s for the table %s!",
serialize( $data_item_attrs ),
$table_name
);
$wpdb->show_errors();
wp_error_log( $message_error, "Insert Data Error" );
return false;
}
return true;
}
new code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
dbDelta( $query_string );
}
My problem is using dbDelta disturbs inserts containing the character ";".
It´s because in dbDelta, there is ";" as delimiter.
How can I fix this ?
Share Improve this question edited Jan 2, 2019 at 14:03 J.BizMai asked Jan 2, 2019 at 13:27 J.BizMaiJ.BizMai 9302 gold badges10 silver badges30 bronze badges 4 |1 Answer
Reset to default 0That happens for this reason :
The function
dbDeltacan receive as first parameter ($queries) an array or a string. If$queriesis a string,dbDeltawill make an array with ";" as delimiter.
inside dbDelta
if ( !is_array($queries) ) {
$queries = explode( ';', $queries );
$queries = array_filter( $queries );
}
So the solution is to make an array of queries instead of a string as a first parameter like this:
myFunction(){
...
$query_string = array(
0 => "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}"
);
dbDelta( $query_string );
}
The answer was found here.
To manage database plugin update, I changed my code using dbDelta.
old code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
$is_data_inserted = $wpdb->query( $query_string );
if( !$is_data_inserted ){
$message_error = sprintf( "impossible to insert the data %s for the table %s!",
serialize( $data_item_attrs ),
$table_name
);
$wpdb->show_errors();
wp_error_log( $message_error, "Insert Data Error" );
return false;
}
return true;
}
new code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
dbDelta( $query_string );
}
My problem is using dbDelta disturbs inserts containing the character ";".
It´s because in dbDelta, there is ";" as delimiter.
How can I fix this ?
To manage database plugin update, I changed my code using dbDelta.
old code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
$is_data_inserted = $wpdb->query( $query_string );
if( !$is_data_inserted ){
$message_error = sprintf( "impossible to insert the data %s for the table %s!",
serialize( $data_item_attrs ),
$table_name
);
$wpdb->show_errors();
wp_error_log( $message_error, "Insert Data Error" );
return false;
}
return true;
}
new code
myfunction(){
...
$query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}";
dbDelta( $query_string );
}
My problem is using dbDelta disturbs inserts containing the character ";".
It´s because in dbDelta, there is ";" as delimiter.
How can I fix this ?
Share Improve this question edited Jan 2, 2019 at 14:03 J.BizMai asked Jan 2, 2019 at 13:27 J.BizMaiJ.BizMai 9302 gold badges10 silver badges30 bronze badges 4- please check this code : myfunction(){ global $wpdb; $query_string = "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $query_string ); } – vikrant zilpe Commented Jan 2, 2019 at 13:43
- codex.wordpress/Creating_Tables_with_Plugins – vikrant zilpe Commented Jan 2, 2019 at 13:52
-
Why are you trying to use
dbDeltafor inserting data? That's not what it's for. It's intended for creating and modifying tabes. To insert data use$wdb->insert(). – Jacob Peattie Commented Jan 2, 2019 at 13:56 - yes jacob is always right – vikrant zilpe Commented Jan 2, 2019 at 13:58
1 Answer
Reset to default 0That happens for this reason :
The function
dbDeltacan receive as first parameter ($queries) an array or a string. If$queriesis a string,dbDeltawill make an array with ";" as delimiter.
inside dbDelta
if ( !is_array($queries) ) {
$queries = explode( ';', $queries );
$queries = array_filter( $queries );
}
So the solution is to make an array of queries instead of a string as a first parameter like this:
myFunction(){
...
$query_string = array(
0 => "INSERT INTO {$table_name} {$str_column_names} VALUES {$str_values}"
);
dbDelta( $query_string );
}
The answer was found here.
本文标签: dbDelta with the character
版权声明:本文标题:dbDelta with the character ; 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749050808a2308587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


dbDeltafor inserting data? That's not what it's for. It's intended for creating and modifying tabes. To insert data use$wdb->insert(). – Jacob Peattie Commented Jan 2, 2019 at 13:56