I am writing a PHP application that works with a MySQL database.  As I am fairly new to PHP I am looking for ways to improve my code.  Below are two functions, create_ct_query and create_ct_query2.  create_ct_query2 is the result of my refactoring create_ct_query.  I am fairly happy with it now, but am wondering how it looks to others.<?php// experiment with versions of create_ct_query$lvb_id_ml=30;       // lvb id$lvb_name_ml=100;    // competition name$lvb_competitions_table =  array (table_name => lvb_competitions,         table_cols =>          array (array ('id', $lvb_id_ml),                array ('name', $lvb_name_ml),                array ('age', 'INT')),         primary_key => array ('id', 'age'));function varchar ($len) {  return VARCHAR( . $len . );}function primary_key ($item) {  return $item .  PRIMARY KEY;}// version currently in scriptfunction create_ct_query ($tdesc) {  $ret_val = CREATE TABLE  . $tdesc['table_name'] .  ( ;  foreach ($tdesc[table_cols] as $row) {    if (is_numeric ($row[1])) {      $ret_val .= $row[0] .   . varchar ($row[1]) . , ;    } else {      $ret_val .= $row[0] .   . $row[1] . , ;    }  }  // add primary key constraint  $ret_val .=  CONSTRAINT pk_ . $tdesc[table_name] .     PRIMARY KEY (;  if (is_array ($tdesc['primary_key'])) {    $ret_val .= implode (,, $tdesc['primary_key']);  } elseif (is_string ($tdesc['primary_key'])) {    $ret_val .= $tdesc[primary_key];  } else {    die (Primary key not of right type in tdesc for  .         $tdesc['table_name'] . \n);  }  $ret_val .= ));  return $ret_val;}function create_ct_query2 ($tdesc) {  $ret_val = CREATE TABLE  . $tdesc['table_name'] .  (;  $ret_val .= cols_desc ($tdesc['table_cols']) . , ;  $ret_val .= primary_key_constraint ($tdesc['table_name'],                                      $tdesc['primary_key']) . ));  return $ret_val;}// create the item description from the item// Example:// item_desc (array ('name', 10));//    'name VARCHAR(10)'// item_desc (array ('name', INT));//    'name INT'function item_desc ($item) {  return   . $item[0] .   .    (is_numeric($item[1]) ? varchar($item[1]) : $item[1]);}// create the column description part of table creation query// Example:// cols_desc (array (array ('id', 10), array ('id2', INT)));//   'id VARCHAR(10), id2 INT'function cols_desc ($table_cols) {  return implode (,, array_map ('item_desc', $table_cols));}// create the primary key constraint part of the table creation query// primary_key_constraint ('name', 'id');//    CONSTRIANT pk_name PRIMARY KEY (id)// primary_key_constraint ('name', array ('id', 'id2'));//    CONSTRAINT pk_name PRIMARY_KEY (id, id2)function primary_key_constraint ($table_name, $primary_key_desc)  {  return  CONSTRAINT pk_ . $table_name .  PRIMARY KEY ( .     (is_array($primary_key_desc) ?      implode (,, $primary_key_desc) :     $primary_key_desc);}echo create_ct_query ($lvb_competitions_table) . \n\n;echo create_ct_query2 ($lvb_competitions_table) . \n\n;
Creating SQL queries for a MySQL database
php;sql;mysql