Advertisements

maybe_add_column

Definition:
function maybe_add_column($table_name, $column_name, $create_ddl) {}

* maybe_add_column() * Add column to db table if it doesn’t exist.
* Returns: true if already exists or on successful completion * false on error

Parameters

  • $table_name
  • $column_name
  • $create_ddl

Source code

function maybe_add_column($table_name, $column_name, $create_ddl) {

	global $wpdb, $debug;

	foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {

		if ($debug) echo("checking $column == $column_name<br />");

		if ($column == $column_name) {

			return true;

		}

	}

	//didn't find it try to create it.

	$q = $wpdb->query($create_ddl);

	// we cannot directly tell that whether this succeeded!

	foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {

		if ($column == $column_name) {

			return true;

		}

	}

	return false;

}

9993

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: