[wp-hackers] Table updater wish

Stephen O'Connor steve at stevarino.com
Thu Jul 29 03:45:18 UTC 2004


Matt Mullenweg wrote:

> I want to be able to use the same code for installation and upgrading. 
> It would take some sort of structured description of the tables and 
> fields we want, probably as a nested array or some sort, and then loop 
> over that. It would check if the desired table existed and if not, 
> then create it. If would check if a field existed and, if not, create 
> it. Then it would check if the field was the correct type and, if not, 
> you get the idea.
>
> Anyone want to tackle this?
>
As far as listing tables, you can use the MySQL Query "SHOW TABLES". 
http://dev.mysql.com/doc/mysql/en/SHOW_TABLES.html

And the fields part I actually attempted in my last plugin :D

function list_fields($table_name) {
	global $wpdb;
	$columns = $wpdb->get_results("SHOW COLUMNS FROM $table_name");
	foreach( $columns as $i=>$column ) {
		$columns[$i] = $column->Field;
	}
	return $columns;
}

It returns an array of fields, but can easily be changed to return an 
associative array of "field_name->field_type". This is exactly how I 
updated my plugin and nobody mentioned any problems.

- Stephen




More information about the hackers mailing list