Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.