Created
May 12, 2016 17:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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