11

How can I check if mysql table field even exists ?

The column name is 'price' and I need to see if it exists.

Haven't understood really how the 'EXISTS' works...

Any examples or ideas ?

Thanks

8 Answers8

23

In PHP:

$fields = mysql_list_fields('database_name', 'table_name');
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);}

if (!in_array('price', $field_array))
{
$result = mysql_query('ALTER TABLE table_name ADD price VARCHAR(10)');
}

This should also help you:

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ‘TEST’ AND COLUMN_NAME = ‘TEST_DATE’)
BEGIN
  ALTER TABLE TEST ADD TEST_DATE DATETIME
END

Or you can do:

Show columns from table like 'string';

There has been a similar question posed on SO here before.

Community
  • 1
  • 1
3

Try:

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TEST' AND COLUMN_NAME = 'Price')
BEGIN
    -- do something, e.g.
    -- ALTER TABLE TEST ADD PRICE DECIMAL
END
miku
  • 181,842
  • 47
  • 306
  • 310
2

Another way of doing it in PHP:

$chkcol = mysql_query("SELECT * FROM `table_name` LIMIT 1"); 
$mycol = mysql_fetch_array($chkcol); 
if(isset($mycol['price'])) 
  echo "Column price exists! Do something...";
wvasconcelos
  • 157
  • 1
  • 3
  • 6
    No, answers to old questions help those of looking. Keep adding better answers to old questions I say. If we are not supposed to "resurrect" old questions, then mods should not close repeated questions. – Gabriel Magana Nov 21 '12 at 13:39
1

I found this very useful. It will list all the tables that has that column name.

SELECT table_name, 
       column_name 
FROM   information_schema.columns 
WHERE  column_name LIKE '%the_column_name%' 
j0k
  • 22,600
  • 28
  • 79
  • 90
Paul van Jaarsveld
  • 1,524
  • 1
  • 10
  • 9
1

Well, one way is to do:

select price from your_table limit 1

If you get an error:

#1054 - Unknown column 'price' in 'field list'

then it does not exists.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
1

well here is a function to check out if a particular column exists or not.

public function detect_column($my_db, $table, $column)
{
    $db = mysql_select_db($my_db); //select the database
    $sql = "SHOW COLUMNS FROM $table LIKE '$column'"; //query
    $result = mysql_query($sql);                     //querying
    if(mysql_num_rows($result) == 0)        //checks the absence of column
        echo "column $column doesn't exist !";
        // write your code here! 
    else
        echo "column $column exists!";
}

well if you are designing a frame work, then this function may come to your aid. This function checks for the presence of column when $flag is set to '1' and absence of column when $flag is set to '0'.

public function detect_column($my_db, $table, $column, $flag)
{
    $this->select_db($my_db); //select the database
    $sql = "SHOW COLUMNS FROM $table LIKE '$column'";
    $result = mysql_query($sql);
    if(mysql_num_rows($result) == $flag)
        return true;
    else
        return false;
} 
0

You could get a description of all the column in your table.

desc your_table;

0

I just done something like this using this function for Wordpress, this for update tables that having new chnages on it

public function alterTable() {

    $table_name = $this->prefix . $this->tables['name'];

    $select = "select * from `{$table_name}` where 0=0 limit 1;";
    $query = $this->db->get_results($select, ARRAY_A);


    if (isset($query[0]) && !key_exists('adv', $query[0])) {
        $sql = "ALTER TABLE `{$table_name}` ADD `me` INT NULL DEFAULT NULL ;";
        $this->db->query($sql);
    }

}

codediesel
  • 43
  • 6