• Resolved Doodlebee

    (@doodlebee)


    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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Doodlebee

    (@doodlebee)

    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.

    Thread Starter Doodlebee

    (@doodlebee)

    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.

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

    What value do you use for $charset_collate???

    thanks in advance,
    Gilberto

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

    Thank you very much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘register_activation_hook to create two tables?’ is closed to new replies.