PHP PDO Verify DB

If you have ever needed to verify, create, or update database schema then you will probably like this snippet (ok, it is a little long to call a snippet). This code assumes that you have a class with a shared PDO.

To verify the database, run verifyDB. If the output is 0, then all is well and nothing needs to happen. If the output is 1 then the database, or one of the tables, needs to be created. If the output is 2 then one or more of tables need to be updated to the current schema.

You may have noticed that none of this allows for the dropping of columns or tables. It would be possible to add that functionality, but with the code above you will have to keep any columns that you would rather drop.