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

    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了