Reputation: 1
how to check wether the column in table exists or not in php
Upvotes: 0
Views: 614
Reputation: 2245
Concretely it depends on your database.
For example with Oracle databases you can query USER_TAB_COLUMNS
or ALL_TAB_COLUMNS
.
There exists however abstract solutions that let you to be not tight to a specific implementation. Zend comes to my mind but there certainly exist others. For example with Zend you can do:
$cols = $table->info(Zend_Db_Table_Abstract::COLS);
Here is the manual for Zend table: http://framework.zend.com/manual/en/zend.db.table.html
Upvotes: 0
Reputation: 1248
$result = mysql_query($query);
if(!$result)
{
print "Error";
}
Essentially, if the column doesn't exist, a result won't be brought up and and the variable won't be set.
Upvotes: 0
Reputation: 8046
You can get the columns of the table and just check if the column you're looking for is available in the result of that.
You can execute mysql_query() to issue an SQL SHOW COLUMNS FROM table [LIKE 'name']. This will give you all columns including specifications.
Upvotes: 5