$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>dbDelta with the character ;|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

dbDelta with the character ;

matteradmin10PV0评论

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 dbDelta for 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
Add a comment  | 

1 Answer 1

Reset to default 0

That happens for this reason :

The function dbDelta can receive as first parameter ($queries) an array or a string. If $queries is a string, dbDelta will 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far