List of PHP native_type's for PDO getColumnMeta()

后端 未结 6 1243
走了就别回头了
走了就别回头了 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条回答
  • 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']];
    }
    
    0 讨论(0)
  • 2020-12-19 13:25

    PDO is not a database abstraction. It is "only" an unified access layer. If you switch to another database system you most likely have to change the code. Each (specific) database driver returns its own set of values and there's no "translation layer" for the driver:decl_type info in pdo beyond the native_type/pdo_type fields in the result of getColumnMeta()

    0 讨论(0)
  • 2020-12-19 13:28

    I had my hand in getting the behavior changed to more closely fit the documentation. You may want to refer to PHP Bug #46508.

    0 讨论(0)
  • 2020-12-19 13:33

    One workaround I've come across would require that you use the table name as its own alias.

    You can call getColumnMeta() and get a unique list of tables and for each table execute a 'DESCRIBE {table}' statement. Match up the column name with the ones from your result set to get the actual MySQL data type.

    It worked for me and my needs anyway...

    0 讨论(0)
  • 2020-12-19 13:41

    It's more work but create the database with column names like vc_20_Last_Name. Then explode the the column name on "_". Position zero contains vc or VARCHAR. Position 1 contains the column width or 20. Position 2 and above contains Last and Name which is self explanatory. You now have the possibility to write a generic function to automatically build the HTML forms for INSERT, UPDATE and DELETE operations. You could pass parameters to exclude (or include if it's easier) fields that you don't / do want to appear. Write the code once and use it for ever. Tough luck if you are stuck with someone else's tables.

    0 讨论(0)
  • 2020-12-19 13:43

    This is one of those areas of PDO that was left intentionally undefined, in order to keep the abstraction light weight.

    PDO does not define a standard representation of types for this method; each driver has it's own idea about what it can return here.

    0 讨论(0)
提交回复
热议问题