0
// Initialization and Hooks
global $wpdb;
global $wp_version;
global $wpmyfirstplugin_version;
global $wpmyfirstplugin_db_version;
global $wpmyfirstplugin_table_name;
global $wp_version;
$wpmyfirstplugin_version = '1.0.1';
$wpmyfirstplugin_db_version = '0.0.1';
$wpmyfirstplugin_table_name = $wpdb->prefix.'imart_setting';

function wpmyfirstplugin_install()
{
    global $wpdb;
    global $wpmyfirstplugin_table_name;
    global $wpmyfirstplugin_db_version;

    // create table on first install
    if($wpdb->get_var("show tables like '$wpmyfirstplugin_table_name'") != $wpmyfirstplugin_table_name) {
        wpmyfirstplugin_createTable($wpdb, $wpmyfirstplugin_table_name);

    //add_option("wpmyfirstplugin_db_version", $wpmyfirstplugin_db_version);
        //add_option("wpmyfirstplugin_configuration", '');
    }

}

function wpmyfirstplugin_createTable($wpdb, $table_name)
{
    $sql = "CREATE TABLE  ".$table_name." (
          main_id bigint(20) NOT NULL auto_increment,
          main_key varchar(255) default NULL,
          main_value longtext,
          PRIMARY KEY  (`imart_id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";

    $results = $wpdb->query($sql);
}


register_activation_hook(__FILE__,'wpmyfirstplugin_createTable');

Above is what I key in for starter. When I activate I get the following:

Warning: Missing argument 2 for wpmyfirstplugin_createTable()

This one point to the execution of wpmyfirstplugin_createTable at

wpmyfirstplugin_createTable($wpdb, $wpmyfirstplugin_table_name);

Fatal error: Call to a member function query() on a non-object

This one point to the line where I run

$results = $wpdb->query($sql);

What did I do wrong actually?

2

You are getting this error because 'wpmyfirstplugin_createTable' is called on register_activation_hook. This hook is called when a plugin is activated from dashboard and before declaring any variable. So when this function runs it does not get the table name parameter.

1

You can use the:

maybe_create_table( string $table_name, string $create_ddl );
// be sure to include this before calling the function. 
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 

https://developer.wordpress.org/reference/functions/maybe_create_table/

0

Please try this code:

function test_db_install() 
{
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
   $sql="
       CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."imart_setting`
     (
          main_id bigint(20) NOT NULL auto_increment,
          main_key varchar(255) default NULL,
          main_value longtext,
          PRIMARY KEY  (`main_id`)
     );";
    dbDelta($sql);

$insert_sql ="
    INSERT INTO `wp_posts`(`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES ('admin','','','[Alumini Dashboard]','Dashbord','','publish','','','','dashbord','','','','','','','','','page','','')";

        dbDelta($insert_sql);
  • 2
    Please add an explanation to how your code works and how it should be used – Pieter Goosen Dec 19 '14 at 7:55
0

Please Try this

// Initialization and Hooks
global $wpdb;
global $wp_version;
global $wpmyfirstplugin_version;
global $wpmyfirstplugin_db_version;
global $wpmyfirstplugin_table_name;
global $wp_version;
$wpmyfirstplugin_version = '1.0.1';
$wpmyfirstplugin_db_version = '0.0.1';
$wpmyfirstplugin_table_name = $wpdb->prefix.'imart_setting';
function wpmyfirstplugin_install(){
global $wpdb;
global $wpmyfirstplugin_table_name;
global $wpmyfirstplugin_db_version;
// create table on first install
if($wpdb->get_var("show tables like '$wpmyfirstplugin_table_name'") != $wpmyfirstplugin_table_name) {
    wpmyfirstplugin_createTable($wpmyfirstplugin_table_name);

}
}
function wpmyfirstplugin_createTable($table_name){
global $wpdb;
$sql = "CREATE TABLE  ".$table_name." (
      main_id bigint(20) NOT NULL auto_increment,
      main_key varchar(255) default NULL,
      main_value longtext,
      PRIMARY KEY  (`imart_id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";

$results = $wpdb->query($sql);
}
register_activation_hook(__FILE__,'wpmyfirstplugin_install');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.