PDO和数据库类 - 获取表模式

时间:2013-06-06 20:46:01

标签: php mysql pdo

我有这个类“数据库”,它扩展了PDO,我可以进行搜索并找到所有这样的完美:

public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC){
    $sth = $this->prepare($sql);
    foreach ($array as $key => $value) {
        $sth->bindValue("$key", $value);
    }

    $sth->execute();
    return $sth->fetchAll($fetchMode);
}

但是现在我想要一个能让我检索表模式的函数......

我尝试使用PDO:cubic_schema,但无法使其正常工作。我不断发现要么找不到常量,要么找不到方法,这是一个尝试......

    public function schema($table) {
    $table_information = $this->cubrid_schema(PDO::CUBRID_SCH_CLASS, $table);
    return $table_information;
}

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:6)

好的,我找到了我的解决方案,根本就不使用Cubric ....

public function schema($table) {

    $q = $this->prepare("SHOW COLUMNS FROM `$table`");
    $q->execute();
    $table_fields = $q->fetchAll();
}

返回:

Array
(
    [0] => Array
        (
            [Field] => dataid
            [0] => dataid
            [Type] => int(11)
            [1] => int(11)
            [Null] => NO
            [2] => NO
            [Key] => PRI
            [3] => PRI
            [Default] => 
            [4] => 
            [Extra] => auto_increment
            [5] => auto_increment
        )

    [1] => Array
        (
            [Field] => text
            [0] => text
            [Type] => varchar(255)
            [1] => varchar(255)
            [Null] => NO
            [2] => NO
            [Key] => 
            [3] => 
            [Default] => 
            [4] => 
            [Extra] => 
            [5] => 
        )

)