我有一个带有自定义表wp_new_term_relationships
的wordpress数据库我需要一个函数将该表中的所有记录追加到表wp_term_relationships
,然后它将删除第一个表中的所有数据,类似的东西:
function jr_cron_insert_jobs(){
global $wpdb, $jr_log;
$sql = $wpdb->prepare("INSERT INTO wp_term_relationships select * from wp_new_term_relationships");
$wpdb->query( $sql );
// and another sql query to delete all data from the table "wp_new_term_relationships"
}
add_action('init', 'jr_cron_insert_jobs');
这个函数不起作用,对mysql数据库没有影响我在error_log
中有这个警告警告:缺少wpdb :: prepare()
的参数2所以我需要一个人为我修改它
答案 0 :(得分:0)
试试这个:
function jr_cron_insert_jobs(){
global $wpdb, $jr_log;
$sql = $wpdb->prepare("INSERT INTO wp_term_relationships select * from wp_new_term_relationships");
$sql->execute();
//AND FOR THE DELETE STATEMENT:
$delete = $wpdb->prepare("DELETE FROM wp_new_term_relationships");
$delete->execute();
}
add_action('init', 'jr_cron_insert_jobs');
PDO :: exec() - 执行SQL语句并返回受影响的行数
PDO :: query() - 执行SQL语句,将结果集作为PDOStatement对象返回
PDOStatement :: execute() - 执行预准备语句
答案 1 :(得分:0)
如果我删除了准备声明
,它的效果很好function jr_cron_insert_jobs() {
global $wpdb, $jr_log;
$sql = $wpdb->query("INSERT INTO wp_term_relationships select * from wp_new_term_relationships");
$sql2 = $wpdb->query("TRUNCATE TABLE wp_new_term_relationships");
}
add_action('added_term_relationship', 'jr_cron_insert_jobs');