Skip to content
scribu edited this page May 3, 2012 · 7 revisions

Using scbTable

<?php
require dirname(__FILE__) . '/scb/load.php';

scb_init( 'my_plugin_init' );

function my_plugin_init() {
	new scbTable( 'example', __FILE__, "
		example_id int(20),
		example varchar(100),
		PRIMARY KEY  (example_id)
	");
}

Note that it uses dbDelta() internally, so there are some restrictions:

  • don't use ticks: ``
  • add two spaces after PRIMARY KEY

This will take care of creating the table upon plugin activation (or upgrading it if it already exists) and uninstalling it when the plugin is deleted.

It also sets $wpdb->example = $wpdb->prefix . 'example';. This is handing doing custom queries:

<?php
$results = $wpdb->get_results( "SELECT * FROM $wpdb->example" );

Using functions

If you're using scbFramework in a theme or if you need to take a more hands-on approach, you can use the underlying functions:

<?php

scb_register_table( 'example' );  // sets up $wpdb->example

function my_install() {
	scb_install_table( 'example', "
		example_id int(20),
		example varchar(100),
		PRIMARY KEY  (example_id)
	");
}

function my_uninstall() {
	scb_uninstall_table( 'example' );
}
Clone this wiki locally