dbDelta with the character ;

admin2025-06-04  1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749050834a315936.html

最新回复(0)