Validation library for PHP / mysql

Is there any lightweight authentication library for PHP that can easily check if a particular string or value is for a known database type -

Something like that:

 if (is_MEDIUMINT($var)) {
      $this->db->insert($anothervar);
 }

Thank!

+5
source share
5 answers

The INFORMATION_SCHEMA database is part of the ANSI 2003 specification, so you can use it for any database provider that supports it (MySQL, Postgresql, SQLite, MSSQL 2k5 +).

/*public static*/ function is_dbtype($table, $column, $type) {
    $db = (...)::getInstance(); // assuming PDO
    $sql = 'SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS '.
        'WHERE TABLE_NAME = :table AND COLUMN_NAME = :column';
    $st = $db->prepare($sql);
    $st->execute(array(':table' => $table, ':column' => $column));
    return ($type == $st->fetchColumn());
}

COLUMN_TYPE DATA_TYPE, "varchar" "varchar (64)". , : IS_NULLABLE, NUMERIC_PRECISION, CHARACTER_SET_NAME ..

( , , is_* . , info_schema , () . , , , .)

MySQL-only alternate: , DESCRIBE [table]. , "bigint" "bigint (21) ", , .

+3

, , is_MEDIUMINT() :

is_MEDIUMINT()
is_MEDIUMINT_NULL()
is_MEDIUMINT_NOTNULL()
is_MEDIUMINT_UNSIGNED()
is_MEDIUMINT_UNSIGNED_NULL()
is_MEDIUMINT_UNSIGNED_NOTNULL()

, SQLite, , INT, MySQL 5 (TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT), ( INTEGER, BOOL, BOOLEAN SERIAL) float, - . , , UNIQUE , .

, , , , , , , - , MySQL:

, , MySQL . 256 TINYINT TINYINT UNSIGNED, MySQL 127 255 .

?

if (is_MEDIUMINT($var)) {
  $this->db->insert($anothervar);
}

else {
  // do what?
}

, INSERT OR IGNORE.

+6

, . , .

, dbms , , , . , , , , , . , :)

+2

, . , , , .

, . ( , /.)

+1

is_* PHP?

is_integer , is_float etc.?

There is also get_type , but it should not be used for type checking

+1
source

All Articles