How do I get the actual max length of a specified column in php (prior to PHP7)?
For instance, this table:
id - int(11) name - string(20)
I want in php to select the maximum number of characters that a field can have, like
SELECT length(name) from table1
and it should then return 20 (since it's the maximum number of characters for that field).
You should use mysql_field_len
http://php.net/manual/en/function.mysql-field-len.php
You can use mysql_field_len for a single column or use the SHOW COLUMNS FROM table query in MySQL to get information about all the columns at once.
Just be careful when using mysql_field_len to read columns that are not ints or strings. For example, decimal(6,4) will return 10 from mysql_field_len.
You may wish to use SHOW COLUMNS and parse the data, if you need all of the column information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With