WordPress.org

Forums

[resolved] DROP plugin table - dbDelta = no good (4 posts)

  1. Michael Fields
    Themer
    Posted 8 years ago #

    Hi.
    This is my first time posting here, so if this kind of topic is not appropriate, please direct me to a better place. I was reading an article in the codex titled Creating Tables with Plugins. This article explains how to create a database table via the activate_ hook. The first thing that came to my mind was "can you delete the table upon deactivate_"? I successfully accomplished this, but not without problems. The dbDelta function does not handle DROP statements therefore I had to hard code the query.

    My question is: "Is it better to code database interaction by hand or by using WordPress's db handling functions?" My plugin's "Deactivate" section looks like this:

    add_action('deactivate_'.$plugin_path,'jal_uninstall');
    function jal_uninstall () {
    global $table_prefix, $table_suffix, $wpdb;
    $table_name = $table_prefix . $table_suffix;
    $sql = "DROP TABLE $table_name;";
    mysql_connect( DB_HOST, DB_USER, DB_PASSWORD );
    mysql_select_db( $table_prefix . DB_NAME );
    mysql_query( $sql );
    }

    Is there a function in WordPress core that is better suited to carry out the DROP?

    Thanks in advance,

    _m

  2. paulfynch
    Member
    Posted 8 years ago #

    I don't know about a better way, but you could use the built in db connector (kuz im fickle :P):

    function jal_uninstall () {
    global $table_prefix, $table_suffix, $wpdb;
    $table_name = $table_prefix . $table_suffix;
    $wpdb->query("DROP TABLE {$table_name}");

    }

  3. cruelladeville
    Member
    Posted 8 years ago #

    What I did was altering the dbDelta function. I added the following lines in the function (before the else condition)

    else if(preg_match("|DROP TABLE ([^ ]*)|", $qry, $matches)) {
    $iqueries[] = $qry;
    }

  4. Michael Fields
    Themer
    Posted 8 years ago #

    Thanks for the replies...
    Sorry it's been so long since I've been here.
    I think that I feel more comfortable using Paul's method.
    I am not one to go hacking away at core files.
    Thanks to both of you for posting!

Topic Closed

This topic has been closed to new replies.

About this Topic