Bug #25360 mysql_fetch_field()->max_length always returns 0
Submitted: 2 Jan 2007 10:46 Modified: 2 Jan 2007 11:38
Reporter: Ian Carter Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.1.14 beta OS:Windows (Windows XP Pro (sp2))
Assigned to: CPU Architecture:Any

[2 Jan 2007 10:46] Ian Carter
Description:
Using PHP 5.2.
The max_length property of the returned object from a call to mysql_fetch_field() always returns 0, regardless of the field type.

How to repeat:
<?php
include_once "./common_db.inc";

$link_id = db_connect();
$result = mysql_list_fields($default_dbname, $user_tablename, $link_id);
$num_fields = mysql_num_fields($result);

for ($i = 0; $i < $num_fields; $i++) {
    $field_info_object = mysql_fetch_field($result, $i);
    
    echo $field_info_object->name . "(" .
         $field_info_object->max_length . ")";
         
    echo " - " . $field_info_object->type;
    
    if ($field_info_object->not_null) {
        echo " not_null ";
    } else {
        echo " null ";
    }
    
    if ($field_info_object->primary_key) {
        echo " primary_key ";
    } else if ($field_info_object->multiple_key) {
        echo " key ";
    } else if ($field_info_object->unique_key) {
        echo " unique ";
    }
    
    if ($field_info_object->unsigned) {
        echo " unsigned ";
    }
    
    if ($field_info_object->zerofill) {
        echo " zero_filled";
    }
    
    echo "<br>";
}
?>
[2 Jan 2007 11:05] Andrey Hristov
ext/mysql is thin wrapper around libmysql. As I see it in sql_parse.cc, COM_FIELD_LIST does not use part of the code path for normal SQL statements. This could be a reason if there is a problem. Just a thought.
[2 Jan 2007 11:38] Georg Richter
From http://www.php.net/mysql_list_fields:

This function is deprecated. It is preferable to use mysql_query() to issue a SQL SHOW COLUMNS FROM table [LIKE 'name'] statement instead.

If you use MySQL 5.0 or above, you can also retrieve metadata from INFORMATION_SCHEMA.