3

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");
3
  • Do you get any error messages? Commented Aug 23, 2012 at 17:20
  • No error messages. The function is working perfectly. The problem is that if one of the fields already exists the error will be returned: #1060 - Duplicate column name 'url' . And then rather than keep on going with the next item in the array the entire function returns false. We need to get around this problem. Is there some way of telling it to continue IF ERROR. Commented Aug 23, 2012 at 17:35
  • You can check INFORMATION_SCHEMA database for column names already in the table and remove them from your array before trying alter statement. Commented Aug 23, 2012 at 18:18

2 Answers 2

2

You could try specifying IGNORE for the query:

$fsdbh->prepare("ALTER IGNORE TABLE `$table` ADD `$field` $type;");
Sign up to request clarification or add additional context in comments.

2 Comments

Does not fix the problem. The SQL returns an error if the column already exists and then exits the function rather than checking the next item in the array. We need to get around this so that it keeps going even if their is an error. Any ideas?
@RobinKnight Could you check what the PDO::ATTR_ERRMODE is set to? Maybe it throws an exception that you have to catch. See prodigitalson's answer.
1

Use a try catch:

function verifydbtable($table,$fields){
    global $fsdbh;
    $errors = array();
    foreach($fields as $field => $type){
        $verify = $fsdbh->prepare("ALTER TABLE `$table` ADD `$field` $type;");
        try {
          $verify->execute();
        } catch (PDOException $e) {
           // you might want to check and make sure its actually the right error
           // but you can figure that out if when you need to do so
           // we'll also store the field name in an array jsut in case we want to use it
           $errors[] = $field;
        }
    }

     return 1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.