Add new column to wordpress database

I am trying to update my plugin. Therefore, I have to update mysql_table. But when you try to create a new column, the program gets an exception.

This is my current table:

$sql = "CREATE TABLE  {$table_name} (
        say_id             int(11)   not null AUTO_INCREMENT,
        customer_mail      text      not null,
        customer_name  text      not null,
        customer_messagge      text      not null,
        messagge_date_time  datetime  not null,

        PRIMARY KEY (say_id)
        )ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

        require_once(ABSPATH . "wp-admin/includes/upgrade.php");
        dbDelta($sql);

Now I add colum to more than one table. I try to execute an Alter table, this works once and adds a column, but another update. I get this error.

This is mycode

$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");

And this is my mistake

WordPress database error: [Duplicate column name "say_state"] ALTER TABLE wp_customer_say ADD say_state INT (1) NOT NULL DEFAULT 1

I see this error and am not trying to do it;

$query          = $wpdb->query("select * from wp_customer_say");
        $respond        = mysql_num_fields( $query );
        $column_array   = array();

        for($i = 0; $i < $respond ; $i++):
            $column_array[]     = mysql_field_name($query,$i);
        endfor;

        if( !in_array("say_state",$column_array) ):
            $wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
        endif;

and I get this error.

Warning: mysql_num_fields() expects parameter 1 to be resource, integer given in

Help me please. Thanks. Sorry bad english.

+4
5

. mysql-standred , :

$row = $wpdb->get_results(  "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'wp_customer_say' AND column_name = 'say_state'"  );

if(empty($row)){
   $wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
}
+10

WordPress, ,

$myCustomer = $wpdb->get_row("SELECT * FROM wp_customer_say");
//Add column if not present.
if(!isset($myCustomer->say_state)){
    $wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
}
+5

, , @Amandeep Wadhawan.

register_activation_hook(__FILE__, 'install_tables');
function install_tables()
{
    update_options('my_plugins_current_db_version', 0);  // Replace 0 with whatever your final database version is
    // Code here to create the final version of your database tables
}

add_action('plugins_loaded', 'update_databases');
public function update_databases()
{
    global $wpdb;
    $prefix = $wpdb->prefix;
    $a_table_to_update = $prefix . $a_table_to_update;

    $current_version = get_option('my_plugins_current_db_version', 0);

    switch($current_version)
    {
        // First update
        case 0:
            // first update code goes here (alter, new table, whatever)
            $current_version++;
        case 1:
            // next update code goes here
            $current_version++;
    }

    update_option('my_plugins_current_db_version', $current_version);

}

, install_tables() , , my_plugins_current_db_version .

update_databases() $current_version .

, , - ... plugins_loaded hook.

, , .

+1

,

$check_column = (array) $wpdb->get_results(  "SELECT count(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME = '{$wpdb->prefix}my_table' AND COLUMN_NAME = 'some_name'"  )[0];

$table_name = $wpdb->prefix . 'my_table';
 $check_column = (int) array_shift($check_column);
 if($check_column == 0) {
 $wpdb->query(
    "ALTER TABLE $table_name
       ADD COLUMN 'some_name' VARCHAR(55) NOT NULL
      ");
  }
+1

Rikesh ( !), , , , $table_prefix wp-config.php, .

// Replace `table_name` with the actual table name
$table = $table_prefix.'table_name';

// And use sprintf to pass the table name
// Alternatively, you can use $table instead of sprintf, 
//     if you use double quotes such as shown here
$myCustomer = $wpdb->get_row( sprintf("SELECT * FROM %s LIMIT 1", $table) );

// If you prefer not using sprintf, make sure you use double quotes
// $myCustomer = $wpdb->get_row( "SELECT * FROM $table LIMIT 1" );

// Replace "missing_field" by the column you wish to add
if(!isset($myCustomer->missing_field)) {
    $wpdb->query( sprintf( "ALTER TABLE %s ADD phone2 VARCHAR(255) NOT NULL", $table) );

    // Alternate example using variable
    // $wpdb->query( "ALTER TABLE $table ADD phone2 VARCHAR(255) NOT NULL") );
}
0
source

Source: https://habr.com/ru/post/1523505/


All Articles