tags:

views:

43

answers:

3

Hello!

How do i get the actual max length of a specified column in php?

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 its the maximum number of characters for that field).

+4  A: 

You should use mysql_field_len

http://php.net/manual/en/function.mysql-field-len.php

newinjs
Note that this effectively prevents you from using the MySQLi or PDO extensions.
Emil Vikström
Why does it prevent those extensions? For me everything freezes, could that be because of MySQLi?
Patrick
+2  A: 

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.

Anax
+2  A: 

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.

David Caunt