CastlesBlog.com

a blog about a blog

Subscribe

Get mySQL Enumeration Values as an Array

I often use enumeration values when defining a list of options in mySQL. In the past I would make sure my web form matched my database and update each form when I change the database.... but not any more.. I've written a quick function that will return the possible values from mySQL as an array. I can then use the result in my forms and the only place that needs to change is the database.

function getEnums($table,$field) {
	$sql = "SELECT
			COLUMN_TYPE
		FROM 
			INFORMATION_SCHEMA.COLUMNS
		WHERE
			TABLE_NAME = '$table'
			AND COLUMN_NAME = '$field'";

	$this->query($sql);

	preg_match_all('/\'(.[^\']+)\'/',$this->get('COLUMN_TYPE'),$matches);

	return $matches[1];
}

I'm using this in my database class. Modify it to suit your needs.

Post Comment