Simple SQL table creation

<?php

global $test_db_version;
$test_db_version = "1.0";

function test_create_db_table() {
global $wpdb;
global $test_db_version;

$table_name = $wpdb->prefix . "test_table";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time varchar(11) NOT NULL,
title text NOT NULL,
body text NOT NULL,
UNIQUE KEY id (id)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);

add_option("test_db_version", $test_db_version);
}
}

function test_add_row(){
global $wpdb;
$table_name = $wpdb->prefix . "test_table";

$test_title = "The Title";
$test_body = "The Body Text";

$rows_affected = $wpdb->insert( $table_name, array( 'time' => date("m-d-Y"), 'title' => $test_title, 'body' => $test_body ) );
}

add_action('init','test_create_db_table');
/* Uncomment this action to initiate a new row
Just replcace wp_login with the hook of your choice
add_action('wp_login','test_add_row');
*/
?>

0 Comments

You must be logged in to post a comment.