Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADD column if NOT exists - possible?

Status
Not open for further replies.

andrewconcord

Technical User
Oct 25, 2002
3
NZ
Is there anyway to write a table maintenance script that will add a column if it doesn't already exist.

I need to maintain many identical tables at different sites, and want a refresh script that will always ensure all the default columns have been created.
 
u may use a function like this:


function b_getFieldList($DB,$table,$linkRecource='') {
if ($linkRecource != '') $result = @mysql_list_fields($DB, $table,$linkRecource);
else $result = @mysql_list_fields($DB, $table);
$columns = @mysql_num_fields($result);
if ( $no ) return $columns;
for($l = 0; $l < $columns; $l++)
$arrFields[] = @mysql_field_name($result, $l);
return $arrFields;
}

$arrFields = b_getFieldList($DB,$table);

// if the field ($fieldname) does not show up
// in the fieldlist, simply add it ...
if ( !in_array($fieldname,$arrFields) ) {
// built a function like this ...
add_field_to_table($fieldname,$table,$position);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top