Skip to content

Instantly share code, notes, and snippets.

@nklatt
Created May 12, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nklatt/6cd2c514b7bf6d5a6aa913359bbe2ae4 to your computer and use it in GitHub Desktop.
Save nklatt/6cd2c514b7bf6d5a6aa913359bbe2ae4 to your computer and use it in GitHub Desktop.
Ensure a column exists in a MySQL table using concrete5, though that is secondary
<?php defined('C5_EXECUTE') or die(_('Access Denied.'));
function addColumnToTable($table, $column, $type = 'tinyint(1) not null default 0') {
$db = Loader::db();
$qResult = $db->Execute('show columns from `'.$table.'` like "'.$column.'"');
$columnAlreadyExists = false;
foreach ($qResult as $row) {
if (is_array($row) && !empty($row['Field']) && $row['Field'] === $column) {
$columnAlreadyExists = true;
}
}
if ( ! $columnAlreadyExists ) {
$db->Execute("alter table $table add column $column $type');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment