tags:

views:

24

answers:

2

MySQL is returning the current field value length in property max_length.

I there a way to get the correct values ?

ie Field part_code is a varchar(32) that returns 3 if it contains the value of "ABC" instead of the expected result of 32

EDIT

original mysql

//--------------------------------------------------------------------------
function build_fields(){
    $idx = 0;
    $num = @mysql_num_fields($this->qry);
    while($idx < $num){
        $field = mysql_fetch_field($this->qry, $idx);
        $this->fields[$field->name] = $field;
        $idx++;
    }
} // function build_fields()
//--------------------------------------------------------------------------

new mysqli

//-------------------------------------------------------------------------- function build_fields(){

foreach(mysqli_fetch_fields($this->qry) as $name => $value){
    $this->fields[$name] = (array) $value;
}

} // function build_fields() //--------------------------------------------------------------------------

Used DESCRIBE table_name and parsed the varchar(32) and int(4,2)

+2  A: 
SELECT CHARACTER_MAXIMUM_LENGTH,
       COLUMN_TYPE 
  FROM `information_schema`.`COLUMNS`
 WHERE TABLE_SCHEMA = 'MySchemaName'
   AND TABLE_NAME = 'MyTableName'
   AND COLUMN_NAME = 'MyColumnName'
Mark Baker
+1: Might want to use [FLUSH TABLES](http://dev.mysql.com/doc/refman/5.0/en/flush.html) prior - INFORMATION_SCHEMA tables can be cached: http://stackoverflow.com/questions/3240164/how-do-i-detect-if-a-table-exist-mysql
OMG Ponies
A: 

In addition to Mark Baker's method , you can also get column information in several other ways:

SHOW  COLUMNS FROM table_name where field='field_name';
// OR
DESCRIBE table_name field_name;

Then you need to parse 'Type' column (it's returned as varchar(32)) to get the max length.

a1ex07