doucan9079 2012-03-23 02:22
浏览 341

如何在pdo中获取字段数据类型?

Below is some code i tried to do this, I am using mysql 5.0+ and innodb engine. Can i use getColumnMeta (experiment ?) and how to use it to get datatype metadata?

Thank you.

 $types = array(
    PDO::PARAM_BOOL => 'bool',
    PDO::PARAM_NULL     => 'null',
    PDO::PARAM_INT      => 'int',
    PDO::PARAM_STR      => 'string',
    PDO::PARAM_LOB      => 'blob',
);

try{

$sql = 'SELECT COUNT(*) FROM subscriber,list_sub WHERE list_sub.ListID =? AND list_sub.SubID = subscriber.SubID  ';

$stmt = $conn->prepare($sql);
$stmt->execute(array($list));
$meta = $stmt->getColumnMeta(0);
var_dump($meta);
}
catch(PDOException $e)
    {
    die ($e->getMessage()."<a href='view.php' onClick='window.location.reload()'> Back</a>"); 
    }
  • 写回答

1条回答 默认 最新

  • douchi0471 2013-08-17 17:07
    关注

    I know this thread is 1+ years old, but if others stumble upon it as I have on a search, it may be useful.

    I have a PDO database wrapper class that I built, and here is my method within my class for doing this. I do not know how portable it is as I have never used oracle, but it works for my situation using MySql. This requires that the DBMS supports "SHOW COLUMNS FROM", so it should work with any DBMS that supports that.

    As can be seen, I use a try/catch block to catch any PDO exceptions that may occour and log the errors.

        /**
     * @ getFieldType - gets the field data type for a specific field in a table.
     *
     * @ param type: text $table = The name oof a data table.
     * @ param type: text $field = A field in the data table.
     */
    public static function getFieldType($table, $field) {
        $ret = false;
        $sql = "SHOW COLUMNS FROM `$table` WHERE Field = '$field'";
    
        try {
            $result = self::dbQueryRow($sql);
            $ret = $result['Type'];
        } catch (PDOException $e) {
            self::logError($e, "getFieldType", $sql);
        }
    
        return $ret;
    } //End public static function getFieldType
    

    The dbQueryRow and logError methods used here should be self explanatory.

    Hope this is helpful

    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!