WordPress.org

Ready to get started?Download WordPress

Forums

Success message for table creation (2 posts)

  1. jalenack
    Member
    Posted 8 years ago #

    Hey, I have a plugin that creates a new table in the wordpress database. It is installed by browsing to a install.php file provided in the plugin. But how it is now, it will just give you a blank page if it installed correctly, and if it didn't then it will throwup mysql errors. I'd like it to give a little "successfully installed" message.

    // Create table
    $sql[] = "CREATE TABLE ".$table_prefix."liveshoutbox (".
    "id mediumint(9) NOT NULL AUTO_INCREMENT, ".
    "time int(11) NOT NULL, ".
    "name tinytext NOT NULL, ".
    "text text NOT NULL, ".
    "UNIQUE KEY id (id))";

    // Run the query
    foreach($sql as $query) {
    $wpdb->query($query);
    }

    There is my installation code. I've tried wrapping it in all sorts of if statements, but none of them turn up anything. I also looked through wordpress's install.php file to see if there was anything I could use, but I didn't find anything. Any ideas?

  2. tdw
    Member
    Posted 8 years ago #

    try this:

    // dummy vars
    $sql = array(
    "SELECT bar FROM foo",
    "SELECT foo FROM bar",
    "SELECT foobar FROM foobar",
    "SELECT * FROM ". $wpdb->posts,
    );


    // stop $wpdb from printing out error messages
    $wpdb->hide_errors();
    // $wpdb error array
    global $EZSQL_ERROR;
    // keep error count
    $cnt = 0;
    // run the query
    foreach($sql as $query) {
    $wpdb->query($query);
    if($cnt < @count($EZSQL_ERROR)) {
    $cnt = count($EZSQL_ERROR);
    // you can print the last error message
    echo $EZSQL_ERROR[$cnt-1][error_str];
    }
    else {
    echo 'success message';
    }
    }

    this will print out:
    Table 'yourdb.foo' doesn't exist
    Table 'yourdb.bar' doesn't exist
    Table 'yourdb.foobar' doesn't exist
    success message

Topic Closed

This topic has been closed to new replies.

About this Topic