smf_db_create_table

Description

Creates a table with the specified column(s), index(es), etc.

Syntax

bool smf_db_create_table (string $table_name, array $columns[, array $indexes[, array $parameters[, string $if_exists[, string $error]]]])



Parameter $table_name

Expected type: String
Description: The name of the table we wish to create/update. SMF automatically adds the db prefix unless $parameters['no_prefix'] is true



Parameter $columns

Expected type: Array of arrays
Description: An array of arrays containing information about the columns we want to add to the table. See db_add_column for more information.



Parameter $indexes

Expected type: Array of arrays
Description: An array of arrays containing information about the indexes we want to add. Each array must have a 'columns' array (columns to add the index on) and an optional 'type' key, which can be 'primary', 'index' or 'unique'. If type isn't specified, a regular key will be created.



Parameter $parameters

Expected type: Array
Description: An array of parameters: 'parameter' => value. Currently only one parameter exists: 'no_prefix'. If true, SMF will not add the db_prefix to the table name.

Array Elements of $parameters
Key Optional Expected type Description
no_prefix yes Boolean If true, SMF won't add the db_prefix.



Parameter $if_exists

Expected type: Enumerated String
Description: What to do if the table already exists.

Possible string values
Allowed values Description
'error' Don't do anything and return false
'ignore' Don't do anything and return true
'overwrite' Drop the table and re-create it with the specified columns and indexes
'update' Adds any missing columns to the table. Default value.
'update_remove' Adds any missing columns and removes any columns that aren't specified in $columns



Parameter $error

Expected type: String
Description: What type of error should be generated if there's a problem. Defaults to 'fatal'



Return value

Expected type: Boolean
Description: True or false depending on several things (see $error for more info)


Notes

  • Can be used to create a table without worrying about schema compatabilities.
  • Will add $db_prefix to the table name unless 'no_prefix' set as a parameter.
  • If the table exists will, by default, do nothing.
  • Builds table with columns as passed to it - at least one column must be sent.
  • The columns array should have one sub-array for each column (see smf_db_add_column)
  • Adds indexes as specified within indexes parameter.
Advertisement: