diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 4617ca12598..72f3a4c7a13 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -421,6 +421,13 @@ function db_table_exists($table) { return db_num_rows(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'")); } +/** + * Check if a column exists in the given table. + */ +function db_column_exists($table, $column) { + return db_num_rows(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", $table, $column)); +} + /** * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to * the SELECT list entry of the given query and the resulting query is returned. diff --git a/includes/database.mysqli.inc b/includes/database.mysqli.inc index 5e829fbd3a9..04f8cc9aa65 100644 --- a/includes/database.mysqli.inc +++ b/includes/database.mysqli.inc @@ -401,6 +401,13 @@ function db_table_exists($table) { return db_num_rows(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'")); } +/** + * Check if a column exists in the given table. + */ +function db_column_exists($table, $column) { + return db_num_rows(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", $table, $column)); +} + /** * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to * the SELECT list entry of the given query and the resulting query is returned. diff --git a/includes/database.pgsql.inc b/includes/database.pgsql.inc index 56a30ee9a13..be5e4447a88 100644 --- a/includes/database.pgsql.inc +++ b/includes/database.pgsql.inc @@ -394,6 +394,13 @@ function db_table_exists($table) { return db_num_rows(db_query("SELECT relname FROM pg_class WHERE relname = '{". db_escape_table($table) ."}'")); } +/** + * Check if a column exists in the given table. + */ +function db_column_exists($table, $column) { + return db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{". db_escape_table($table) ."}' AND attname='%s'", $column)); +} + /** * Verify if the database is set up correctly. */