Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function create_table($name, $if_not_exists = FALSE, $charset = "UTF8")
- {
- if( empty($this->cols) )
- {
- show_error('You must first add columns and keys before creating a table', FALSE, E_ERROR);
- return FALSE;
- }
- // NOW, we can continue with creating the table
- $sql = 'CREATE TABLE ';
- // Add IF NOT EXISTS if set to true
- if($if_not_exists == TRUE)
- {
- $sql .= 'IF NOT EXISTS ';
- }
- // Add the table name
- $sql .= "`$name` (". PHP_EOL;
- // Loop through and add each column to the sql
- foreach($this->cols as $name => $col)
- {
- if($col['type'] != 'text')
- {
- $sql .= "`$name` ". $col['type'] ."(". $col['size'] .")";
- }
- else
- {
- $sql .= "`$name` text";
- }
- // Set allow Null
- if($col['allow_null'] == FALSE || $col['auto_increment'] == TRUE)
- {
- $sql .= " NOT NULL";
- }
- // Set auto increment
- if($col['auto_increment'] == TRUE)
- {
- $sql .= " AUTO_INCREMENT";
- }
- // Add default
- else
- {
- ($col['default'] === NULL) ? $d = 'NULL' : $d = "'". $col['default'] ."'";
- $sql .= " DEFAULT ". $d;
- }
- // Add comment if one exists
- if($col['comment'] != NULL)
- {
- $sql .= " COMMENT '". $col['comment'] ."'";
- }
- // Add ending
- $sql .= ",". PHP_EOL;
- }
- // Add primary keys
- if(!empty($this->keys))
- {
- $sql .= "PRIMARY KEY (";
- foreach($this->keys as $key)
- {
- $sql .= "`$key`";
- }
- $sql .= ")". PHP_EOL;
- }
- // Finish
- $sql .= ") DEFAULT CHARSET=". $charset .";";
- }
Add Comment
Please, Sign In to add comment