WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] register_activation_hook to create two tables? (5 posts)

  1. Doodlebee
    Member
    Posted 5 years ago #

    Hey all,

    I'm having an issue. I can successfully create a single table (for a plugin I'm writing) in the WP database. however, I need to create *two* tables - not just one. However, I can only get one to take.

    Basically, I'm trying to pull this off:

    function install_tables () {
       global $wpdb;
    
       $sales = $wpdb->prefix . "sales";
       $settings = $wpdb->prefix . "settings"; 
    
       if($wpdb->get_var("show tables like '$sales'") != $sales) {
    
          $sql = "CREATE TABLE " . $sales . " (
               // blah blah whole bunch of info here
          );
                   CREATE TABLE " . $settings . " (
               // blah blah second table info here
          ); ";
    
          require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
          dbDelta($sql);
        }
    }
    
    register_activation_hook(__FILE__,'install_tables');

    Okay, so the "sales" table is creating just fine, but the "settings" is being completely ignored. I've tried different variations to get this to work, and can't.

    Anyone know what I'm doing wrong?

  2. Doodlebee
    Member
    Posted 5 years ago #

    Okay I've been trying to get this to work, and I just cannot see why it's creating one and not the other. So now I've tried this:

    function install_tables() {
       global $wpdb;
       require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
       $sales = $wpdb->prefix . "sales";
       $settings = $wpdb->prefix . "settings"; 
    
       if($wpdb->get_var("show tables like '$sales'") != $sales) {
    
          $sql = "CREATE TABLE " . $sales . " (
               // blah blah whole bunch of info here
          );
    
          dbDelta($sql);
       }
    
       if($wpdb->get_var("show tables like '$settings'") != $settings) {
          $sql = CREATE TABLE " . $settings . " (
               // blah blah second table info here
          ); ";
    
          dbDelta($sql);
        }
    }
    
    register_activation_hook(__FILE__,'install_tables');

    I'm still having no table-love. It's creating the $sales table, but still won't create the $settings. What is it that I'm missing? I know this *can* be done. In fact, I got the "new trial" from another plugin that uses the exact same format to create multiple tables - but it's not doing it.

    I've also tried using $sql for the $sales, and $query for the settings, in case what's happening is it's overriding rather than combining. But still no luck. What is it that I'm missing? Any help would be appreciated.

  3. Doodlebee
    Member
    Posted 5 years ago #

    Okay, I finally found a solution.

    function install_tables() {
      global $wpdb;
      require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
    
      $num = 0;
      $it_tables[$num]['table_name'] = $wpdb->prefix . 'sales';
      $it_tables[$num]['table_sql'] = "CREATE TABLE " . $wpdb->prefix . "sales (
      	//info here
    	) $charset_collate;";  
    
      $num++;
      $it_tables[$num]['table_name'] = $wpdb->prefix . 'settings';
      $it_tables[$num]['table_sql'] = "CREATE TABLE " . $wpdb->prefix . "settings (
      	//info here
    	) $charset_collate;";
    
      foreach($it_tables as $it_table) {
        if(!$wpdb->get_var("SHOW TABLES LIKE '{$it_table['table_name']}'")) {
          $wpdb->query($it_table['table_sql']);
    		}
      }
    }

    This, I placed in a separate file called "tableinstall.php", and in the plugin "index" page, I included this:

    include_once(dirname (__FILE__) . '/tableinstall.php');
    register_activation_hook(__FILE__,'install_tables');

    This seems to be working just fine. Hope it helps someone else.

  4. Microman
    Member
    Posted 4 years ago #

    Hi, I have the same problem, I will try your solution.

    What value do you use for $charset_collate???

    thanks in advance,
    Gilberto

  5. Microman
    Member
    Posted 4 years ago #

    It worked!!! 6 tables and everything is OK!

    Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic