Looking for a way to check whether a table in a database has been assembled with the columns it required.
I build this function:
function verifydbtable($table,$fields){
global $fsdbh;
foreach($fields as $field => $type){
$verify = $fsdbh->prepare("ALTER TABLE `$table` ADD `$field` $type;");
$verify->execute();
}
return 1;
}
Where $table is the table to update and $fields contains an array of all the fields to check. $type contains the type of field eg. VACHAR(20).
The function works for each field until it comes across a field that already exists. Then it returns false.
The problem is that it then won't check the remaining columns from the array. How do we make it continue checking the remaining fields.
Caused by the fact that if a column already exists it will return an error #1060 - Duplicate column name 'subtitle' which is what it should do. We then need to skip to the next item in the array if this error is returned.
In layman's terms we need this but of course this is not a mySQL statement.
prepare("ALTER TABLE `$table` ADD IF NOT EXIST `$field` $type");