Topic awaiting preservation: mysql_fetch_fields as field_name => max_length array(); (Page 1 of 1) |
|
---|---|
Bipolar (III) Inmate From: |
posted 06-23-2009 15:00
What i am trying to do is send query to the database that brings back the field names and their max lengths: code: CREATE TABLE `table` ( `id` int(11) unsigned NOT NULL auto_increment, `sUsername` varchar(35) NOT NULL default '', `sPassword` varchar(40) NOT NULL default '', `sFName` varchar(35) NOT NULL default '', `sMName` varchar(35) default NULL, `sLName` varchar(35) NOT NULL default '', `sAddr1` varchar(40) NOT NULL default '', `sAddr2` varchar(35) default NULL, `sCity` varchar(35) NOT NULL default '', `sState` varchar(35) NOT NULL default '', `sPCode` varchar(35) NOT NULL default '', `cCountryCode` char(2) NOT NULL default '', `sPhone` varchar(30) NOT NULL default '', `sEmail` varchar(80) NOT NULL default '', `sQuestion` varchar(255) default NULL, `sAnswer` varchar(255) NOT NULL default '', `dtInserted` timestamp NOT NULL, `dtUpdated` datetime default NULL, `iAccessLevel` tinyint(4) NOT NULL default '1', `is_banned` tinyint(4) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB;
code: # select all fields $field = mysql_query("SELECT * FROM table"); # setting up array to catch values $db_fields = array(); #getting the number of fields $num_fields = mysql_num_fields($field); # setting the array values to table fields $i = 0; while ($i < $num_fields) { $meta = mysql_fetch_field($field, $i); $db_fields[$meta->name] = $meta->max_length; $i++; } print_r($db_fields);
code: Array ( [id] => 2 [sUsername] => 7 [sPassword] => 40 [sFName] => 6 [sMName] => 8 [sLName] => 7 [sAddr1] => 14 [sAddr2] => 6 [sCity] => 10 [sState] => 2 [sPCode] => 6 [cCountryCode] => 2 [sPhone] => 12 [sEmail] => 22 [sQuestion] => 19 [sAnswer] => 9 [dtInserted] => 19 [dtUpdated] => 19 [iAccessLevel] => 1 [is_banned] => 1 )
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 06-23-2009 16:01
have a look at the mysql DESCRIBE TABLE statement. |
Bipolar (III) Inmate From: |
posted 06-23-2009 16:46
quote:
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 06-24-2009 10:18
code without checking... I'm busy, but DESCRIBE TABLE get's you all the necessary information queried, then all you need to do is parse that code: $query = "DESCRIBE TABLE myTable"; $hRes = mysql_query($query); $arrFields = array(); while ( $arrRow = mysql_fetch_array($query)) { if (strpos($arrRow['Type'],'(') !== false) { $iMaxSize = substr($arrRow['Type'],strpos($arrRow['Type'],'(') + 1, -1); //don't know if php finally does -1 as a length... cut of last character anyhow. Don't forgot to cast to int..., I'm not doing that right now. } else $iMaxSize = false; $arrFields[$arrRow['Field']] = $iMaxSize; }
|
Bipolar (III) Inmate From: |
posted 06-25-2009 00:21
Thank you I'll use that as my starting point and try to make it work for me hopefully I get it right. Thanks again. |
Bipolar (III) Inmate From: |
posted 06-27-2009 23:42
Hey thanks again for the starting point you gave me. I am happy to say i came up with a solution. If anyone is wondering what i came up with here it is: code: DROP TABLE IF EXISTS `table`; CREATE TABLE `table` ( `id` int(11) unsigned NOT NULL auto_increment, `sUsername` varchar(35) NOT NULL default '', `sPassword` varchar(40) NOT NULL default '', `sFName` varchar(35) NOT NULL default '', `sMName` varchar(35) default NULL, `sLName` varchar(35) NOT NULL default '', `sAddr1` varchar(40) NOT NULL default '', `sAddr2` varchar(35) default NULL, `sCity` varchar(35) NOT NULL default '', `sState` varchar(35) NOT NULL default '', `sPCode` varchar(35) NOT NULL default '', `cCountryCode` char(2) NOT NULL default '', `sPhone` varchar(30) NOT NULL default '', `sEmail` varchar(80) NOT NULL default '', `sQuestion` varchar(255) default NULL, `sAnswer` varchar(255) NOT NULL default '', `dtInserted` timestamp NOT NULL, `dtUpdated` datetime default NULL, `iAccessLevel` tinyint(4) NOT NULL default '1', `is_banned` tinyint(4) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB;
code: $sql = "DESCRIBE table"; $fields = mysql_query($sql); $arrFields = array(); while ( $arrRow = mysql_fetch_object($fields) ) { # fields with lengths will have the '(' # get the position of the number after the '(' $pos = strpos($arrRow->Type,'(') + 1; # get the position of the number before the ')' $pos2 = strpos($arrRow->Type,')'); #get the number if digits for the length $length = $pos2 - $pos; # set maxlength to the number inside the '()' $iMaxsize = substr($arrRow->Type, $pos, $length); # if the field has a mex length then place the field $key and $value into the array; $arrFields[$arrRow->Field] = $iMaxsize; # if there is no '(' then the field does not have a max length if (strpos($arrRow->Type,'(') == '') { # unset the array value if it doesnt have a max length unset($arrFields[$arrRow->Field]); } } print_r($arrFields);
|