Built in function of ‘mysql_list_fields’ can be used to check if column exists in table, this function returns fields in table as result set that be traversed to check if particular column exists.
Below is simple function to check if particular column exists in your MySql table.
//checkField function takes table name and column name as parameter
<?php
function checkField($tableName,$columnName)
{
//Global variable, $db for database name, these variable are common in whole application so stored in a global variable, alternatively you can pass this as parameters of function.
global $db;
//Getting table fields through mysql built in function, passing db name and table name
$tableFields = mysql_list_fields($db, $tableName);
//loop to traverse tableFields result set
for($i=0;$i<mysql_num_fields($tableFields);$i++){
//Using mysql_field_name function to compare with column name passed. If they are same function returns 1
if(mysql_field_name($tableFields, $i)==$columnName)
return 1;
} //end of loop
} //end of function
?>