PDA

View Full Version : PHP mysql maximum field width problem



s1nykuL
01-13-2011, 12:31 PM
I am trying to write a generic function which will display the fields of any table in a HTML form. Pretty easy, but I am having problems finding the maximum length of data that can be placed in a column. I want this value so I can constrain the length of data typed into a form input so as not to overflow the allowed data length of the column/field. Of course I know the maximum lengths but I do not want to hard code them into the form input boxes, I want the function to work with any table therefore I need to get them from the table metadata.

For instance field widths are specified in the table design such as varchar(20). When I use the mysqli_fetch_fields_direct function to get the field metadata;



$fieldmax=mysqli_fetch_field_direct($result, column number)->length;

The values returned for numeric fields are correct but the values returned for varchar fields are exactly 3x the length specified in the table design.

If a column is a varchar(20) the length returned is 60 not 20.
After much searching and trying various methods I am stumped.

Can anyone shed some light on this?