List of PHP native_type's for PDO getColumnMeta()

后端 未结 6 1259
走了就别回头了
走了就别回头了 2020-12-19 12:42

I am using the PDO Database Abstraction library to make sure my code is portable. However, I now find that I need column information so I turned to the PDOStatement->getColu

6条回答
  •  Happy的楠姐
    2020-12-19 13:24

    I thought that I could share what I have so far. Since native_type and pdo_type return such drastically different values - I am using the "len" to try to test for strings vs text since everything less than 255 is a var char, int, or boolean. Also, pdo_type only has 5 possible values.

    //PDO data types
    $types = array(
        PDO::PARAM_BOOL => 'bool',
        PDO::PARAM_NULL => 'null',
        PDO::PARAM_INT  => 'int',
        PDO::PARAM_STR  => 'string',
        PDO::PARAM_LOB  => 'blob',
        PDO::PARAM_STMT => 'statement'  //Not used right now
    );
    
    //Get meta
    $column = $result->getColumnMeta(1);
    
    //If the column lenght isn't set - default to ZERO
    $column['len'] = isset($column['len']) ? $column['len'] : 0;
    
    //HACK: If it is longer than 255 chars then it is a text area
    if($column['len'] > 255) {
        $column['type'] = 'text';
    } else {
        $column['type'] = $types[$column['pdo_type']];
    }
    

提交回复
热议问题