Wordpress如何在插件激活上创建表

Here is my code, I write create query into function and call that in register_activation_hook() function, but it's not working.

function test_contact_form() {      
        global $wpdb; 
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        mysql_select_db(DB_NAME);        
        if (!$connection){ die('Error: ' . mysql_error());}

    require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );

    $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
    if( $wpdb->get_var( "SHOW TABLES LIKE '$db_table_name'" ) != $db_table_name ) {
        if ( ! empty( $wpdb->charset ) )
            $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
        if ( ! empty( $wpdb->collate ) )
            $charset_collate .= " COLLATE $wpdb->collate"; 
        $sql = "CREATE TABLE " . $db_table_name . " (
            id int(11) NOT NULL auto_increment,
                        ip varchar(15) NOT NULL,
                        name varchar(60) NOT NULL,
                        emailid varchar(200) NOT NULL,
                        mobileno varchar(10) NOT NULL,
                        message varchar(1000) NOT NULL,
                        PRIMARY KEY (id) 
        ) $charset_collate;";


dbDelta( $sql ); // create query 
        }  
    }
    register_activation_hook(__FILE__, 'test_contact_form');

May this will help you ..

function test_contact_form()
{      
  global $wpdb; 
  $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
  $charset_collate = $wpdb->get_charset_collate();

  $sql = "CREATE TABLE $db_table_name (
                id int(11) NOT NULL auto_increment,
                ip varchar(15) NOT NULL,
                name varchar(60) NOT NULL,
                emailid varchar(200) NOT NULL,
                mobileno varchar(10) NOT NULL,
                message varchar(1000) NOT NULL,
                UNIQUE KEY id (id)
        ) $charset_collate;";

   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );
   add_option( 'test_db_version', $test_db_version );
} 

register_activation_hook( __FILE__, 'test_contact_form' );

The hook "register_activation_hook" will be trigger on plugin activation when you hit Activate link in dashboard.

Your code is ok but it will not work in "function.php" file. "register_activation_hook" will work when you creating plugin.

OR

If you want to create table from "function.php" file. You can simply use 'init' action hook. Just Like that

add_action('init','test_contact_form');

You simply replace your last line with above one

you have written this lines ,

 $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        mysql_select_db(DB_NAME);        
        if (!$connection){ die('Error: ' . mysql_error());}

that's not needed. And you don't need to check for $wpdb variables in if.

Look at below linked question and their answer for your question's solution. https://stackoverflow.com/a/29890105/2219158

I have modified @SelvaKumar answer. As in it once plugin gets activated, a $db_table_name table will be created into WordPress. If someone deactivates plugin then activates the plugin this tries to create $db_table_name table again and will raise an error.

In this code, we need to put a check if a table already exists then this script should not more.

function test_contact_form()
{      
  global $wpdb; 
  $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
  $charset_collate = $wpdb->get_charset_collate();

 //Check to see if the table exists already, if not, then create it
if($wpdb->get_var( "show tables like '$db_table_name'" ) != $db_table_name ) 
 {
       $sql = "CREATE TABLE $db_table_name (
                id int(11) NOT NULL auto_increment,
                ip varchar(15) NOT NULL,
                name varchar(60) NOT NULL,
                emailid varchar(200) NOT NULL,
                mobileno varchar(10) NOT NULL,
                message varchar(1000) NOT NULL,
                UNIQUE KEY id (id)
        ) $charset_collate;";

   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );
   add_option( 'test_db_version', $test_db_version );
 }
} 

register_activation_hook( __FILE__, 'test_contact_form' );