Get Defined Field Length

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zglmb
Forum Newbie
Posts: 1
Joined: Mon Feb 04, 2008 10:18 pm

Get Defined Field Length

Post by zglmb »

I'm new to php/mySQL but I have a lot of experience in asp/SQL Server. In asp, I use the function rst.fields(myFieldName).definedsize, where rst is a recordset and myFieldName is a field name, to find the maximum number of characters that can be entered into text fields. I use this number to set the maxlength property of text boxes in a function that dynamically builds text boxes for my forms.

The closest thing I could find in php/mySQL is mysql_fetch_field but this is causing me two problems. 1. it returns the max length of the longest string in the field, not the defined size and 2. it requires the field index not the field name.

Does any one know how to return the defined field size of a field using the field name?

Thanks in advance.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Get Defined Field Length

Post by Benjamin »

I believe you're wanting the table description:
mysql> describe tables_priv;
+-------------+-------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Table_name | char(64) | NO | PRI | | |
| Grantor | char(77) | NO | MUL | | |
| Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| Table_priv | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter','Create View','Show view') | NO | | | |
| Column_priv | set('Select','Insert','Update','References') | NO | | | |
+-------------+-------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-------+
8 rows in set (0.00 sec)
You'll have to parse out the Type field.
Post Reply