Forums

[resolved] Plugin activated, but tables are not created (7 posts)

  1. freelancer_loken
    Member
    Posted 3 years ago #

    Hi,

    It's been a week since I have been struggling with this problem. I have a plugin that should create its own custom database tables.
    Here is a snippet of my code

    global $wpdb;
    
    function create_load_table(){
    $table_name = $wpdb->prefix.'name';
    
    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){
    $create_name_table_query = "CREATE TABLE ".$table_name."(
    Name_Key int(8) NOT NULL AUTO_INCREMENT,
    Firstname char(25) NOT NULL,
    Middlename char(25) NOT NULL,
    Lastname char(25) NOT NULL,
    Birthday date NOT NULL,
    PRIMARY KEY  ('Name_Key')
    );";
    
    require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
    dbDelta($create_name_table_query);
    }
    
    register_activation_hook(__FILE__,'create_name_table');

    It get's activated whenever I click the activate option, but when I check the database, no tables are created. I am testing this one on localhost before I deploy to our actual webserver.

    Can anyone tell me what I am doing wrong. I have tried a lot of things, and it's been driving me nuts..

    Any help would gladly be appreciated.

    Thanks in anticipation,

    Loken

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Shouldn't this:

    register_activation_hook(__FILE__,'create_name_table');

    be

    register_activation_hook(__FILE__,'create_load_table');
  3. freelancer_loken
    Member
    Posted 3 years ago #

    My bad

    I think I had a typo error
    Ok, I have changed it to the function to be called at activation but it still does not create any table

  4. riledhel
    Member
    Posted 3 years ago #

    Change your code to this one and try again:

    function create_load_table(){
    global $wpdb;
    $table_name = $wpdb->prefix.'name';
    
    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){
    $create_name_table_query = "CREATE TABLE ".$table_name."(
    Name_Key int(8) NOT NULL AUTO_INCREMENT,
    Firstname char(25) NOT NULL,
    Middlename char(25) NOT NULL,
    Lastname char(25) NOT NULL,
    Birthday date NOT NULL,
    PRIMARY KEY  ('Name_Key')
    );";
    
    require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
    dbDelta($create_name_table_query);
    }
    
    register_activation_hook(__FILE__,'create_load_table');

    I think it's a scope problem. Your defining $wpdb outside the scope of the function where you're using it, so PHP just creates an empty variable $wpdb in the function's scope. Hope it helps.

  5. freelancer_loken
    Member
    Posted 3 years ago #

    ok, i will try that. so would that mean that if i intend to use the variable sometime within another function scope, it's assumed that it's contains the previously stored value as declared outside the function scope?

  6. riledhel
    Member
    Posted 3 years ago #

    Let's clarify:

    global $example;
    
    function a(){
        echo $example; //prints nothing. $example isn't in the same scope, so a variable $example is created with '' as it's value.
    }
    
    function b(){
        global $example;
        echo $example; //prints the value of $example.
        $example = 'Hello World';
    }
    
    function c(){
        global $example;
        echo $example; //prints 'Hello World' because the $example value was changed in the b function and it's a global variable.
    }
    
    a();
    b();
    c();
  7. freelancer_loken
    Member
    Posted 3 years ago #

    Ok, thanks a lot for clarifying that one for me.
    It's a lot of help...

    Thanks,

    Loken

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.