/** * Reads extened MySQL field information * * Reads extened field information for each field of a MySQL table * and fills an array like * array( * [Fieldname][attribut]=value, * ['primary_key']=keys * ) * * @param $db Database * @param $table Table * @return array Field infos */ function getExtendedFieldInfo($db, $table) { global $config, $dbo; $fields_infos = array(); //$t = GetCreateTable($db, $table); $sqlf = "SHOW FULL FIELDS FROM `{$db}`.`{$table}`;"; $res = $dbo->query($sqlf, MsdDbFactory::ARRAY_ASSOC); $num_fields = sizeof($res); $f = array(); //will hold all info for ($x = 0; $x < $num_fields; $x++) { $row = $res[$x]; //mysql_fetch_array($res, MYSQL_ASSOC); $i = $row['Field']; // define name of field as index of array //define field defaults - this way the index of the array is defined anyway $f[$i]['field'] = ''; $f[$i]['collation'] = ''; $f[$i]['comment'] = ''; $f[$i]['type'] = ''; $f[$i]['size'] = ''; $f[$i]['attributes'] = ''; $f[$i]['null'] = ''; $f[$i]['default'] = ''; $f[$i]['extra'] = ''; $f[$i]['privileges'] = ''; $f[$i]['key'] = $row['Key']; //array(); if (isset($row['Collation'])) { $f[$i]['collate'] = $row['Collation']; } if (isset($row['COLLATE'])) { $f[$i]['collate'] = $row['COLLATE']; } // MySQL <4.1 if (isset($row['Comment'])) { $f[$i]['comment'] = $row['Comment']; } if (isset($row['Type'])) { $f[$i]['type'] = $row['Type']; } if (isset($row['Field'])) { $f[$i]['field'] = $row['Field']; } $f[$i]['size'] = get_attribut_size_from_type($f[$i]['type']); // remove size from type for readability in output $f[$i]['type'] = str_replace('(' . $f[$i]['size'] . ')', '', $f[$i]['type']); // look for attributes, everthing behind the first space is an atribut $attributes = explode(' ', $f[$i]['type'], 2); if (isset($attributes[1])) { // we found attributes unset($attributes[0]); // delete type $f[$i]['attributes'] = trim(implode(' ', $attributes)); //merge all other attributes // remove attributes from type $f[$i]['type'] = trim(str_replace($f[$i]['attributes'], '', $f[$i]['type'])); } if (isset($row['NULL'])) { $f[$i]['null'] = $row['NULL']; } if (isset($row['Null'])) { $f[$i]['null'] = $row['Null']; } if (isset($row['Default'])) { $f[$i]['default'] = $row['Default']; } if (isset($row['Extra'])) { $f[$i]['extra'] = $row['Extra']; } if (isset($row['Privileges'])) { $f[$i]['privileges'] = $row['Privileges']; } if (isset($row['privileges'])) { $f[$i]['privileges'] = $row['privileges']; } } return $f; }
/** * Reads extened MySQL field information * * Reads extened field information for each field of a MySQL table * and fills an array like * array( * [Fieldname][attribut]=value, * ['primary_key']=keys * ) * * @param $db Database * @param $table Table * @return array Field infos */ function getExtendedFieldInfo($db, $table) { global $config; $fields_infos = array(); $t = GetCreateTable($db, $table); $sqlf = "SHOW FULL FIELDS FROM `{$db}`.`{$table}`;"; $res = MSD_query($sqlf); $num_fields = mysql_num_rows($res); $f = array(); //will hold all info for ($x = 0; $x < $num_fields; $x++) { $row = mysql_fetch_array($res, MYSQL_ASSOC); //v($row); $i = $row['Field']; // define name of field as index of array //define field defaults - this way the index of the array is defined anyway $f[$i]['field'] = ''; $f[$i]['collation'] = ''; $f[$i]['comment'] = ''; $f[$i]['type'] = ''; $f[$i]['size'] = ''; $f[$i]['attributes'] = ''; $f[$i]['null'] = ''; $f[$i]['default'] = ''; $f[$i]['extra'] = ''; $f[$i]['privileges'] = ''; $f[$i]['primary_keys'] = array(); if (isset($row['Collation'])) { $f[$i]['collate'] = $row['Collation']; } if (isset($row['COLLATE'])) { $f[$i]['collate'] = $row['COLLATE']; } // MySQL <4.1 if (isset($row['Comment'])) { $f[$i]['comment'] = $row['Comment']; } if (isset($row['Type'])) { $f[$i]['type'] = $row['Type']; } if (isset($row['Field'])) { $f[$i]['field'] = $row['Field']; } $f[$i]['size'] = get_attribut_size_from_type($f[$i]['type']); // remove size from type for readability in output $f[$i]['type'] = str_replace('(' . $f[$i]['size'] . ')', '', $f[$i]['type']); // look for attributes, everthing behind the first space is an atribut $attributes = explode(' ', $f[$i]['type'], 2); if (isset($attributes[1])) { // we found attributes unset($attributes[0]); // delete type $f[$i]['attributes'] = trim(implode(' ', $attributes)); //merge all other attributes // remove attributes from type $f[$i]['type'] = trim(str_replace($f[$i]['attributes'], '', $f[$i]['type'])); } if (isset($row['NULL'])) { $f[$i]['null'] = $row['NULL']; } if (isset($row['Null'])) { $f[$i]['null'] = $row['Null']; } if (isset($row['Default'])) { $f[$i]['default'] = $row['Default']; } if (isset($row['Extra'])) { $f[$i]['extra'] = $row['Extra']; } if (isset($row['Privileges'])) { $f[$i]['privileges'] = $row['Privileges']; } if (isset($row['privileges'])) { $f[$i]['privileges'] = $row['privileges']; } } // now get key definitions of the table and add info to field-array $sql = 'SHOW KEYS FROM `' . $db . '`.`' . $table . '`'; $res = MSD_query($sql); while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { //echo "<br>Keys of $table: ";v($row); $key_name = isset($row['Key_name']) ? $row['Key_name'] : ''; $index_type = isset($row['Index_type']) ? $row['Index_type'] : ''; $column_name = isset($row['Column_name']) ? $row['Column_name'] : ''; // to do: add other info about index etc. if ($key_name == 'PRIMARY') { $f['primary_keys'][] = $column_name; } } return $f; }