WordPress.org

Support

Support » Plugins and Hacks » [Resolved] DROP plugin table – dbDelta = no good

[Resolved] DROP plugin table – dbDelta = no good

  • 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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}”);

    }

    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;
    }

    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] DROP plugin table – dbDelta = no good’ is closed to new replies.
Skip to toolbar