Is there any lightweight validation library available for PHP that easily can check if a specific string or value is valid for a known database type –
Something like this:
if (is_MEDIUMINT($var)) {
$this->db->insert($anothervar);
}
Thanks!
The INFORMATION_SCHEMA database is part of the ANSI 2003 specification, so you could use that across any DB vendor that supports it (MySQL, Postgresql, SQLite, MSSQL 2k5+).
Change COLUMN_TYPE to DATA_TYPE if you just want “varchar” instead of “varchar(64)”. If you need more, there’s plenty: IS_NULLABLE, NUMERIC_PRECISION, CHARACTER_SET_NAME, etc.
(Not sure I’d use personally use this though, the
is_*functions usually do enough without an extra database call. More importantly, info_schema holds the structure of every database on the server, so granting read permissions to it might (should) be a big deal. If you’re on a shared host you likely won’t have access to it at all.)MySQL-only alternate: do similar but with
DESCRIBE [table]. It’s pretty explicit though, you’ll have to fish out the “bigint” in “bigint(21) unsigned” yourself if that’s all you want.