【问题标题】:getting column names in mssql pdo在 mssql pdo 中获取列名
【发布时间】:2013-02-05 03:15:22
【问题描述】:

我正在从一些 MS SQL PDO 查询构建一个 HTML 表。或试图。我遇到的第一个障碍是我无法获取特定表的列名。找到here,我已经尝试过解决方法

function getColumnNames(){ 

$sql = "select column_name from information_schema.columns where table_name = 'myTable'";
#$sql = 'SHOW COLUMNS FROM ' . $this->table; 

$stmt = $this->connection->prepare($sql); //this is the line that triggers the error

try {     
    if($stmt->execute()){ 
        $raw_column_data = $stmt->fetchAll(PDO::FETCH_ASSOC); 

        foreach($raw_column_data as $outer_key => $array){ 
            foreach($array as $inner_key => $value){ 
                        if (!(int)$inner_key){ 
                            $this->column_names[] = $value; 
                        } 
            } 
        } 
        } 
        return $this->column_names; 
    } catch (Exception $e){ 
            return $e->getMessage(); //return exception 
    }         
}  

getColumnNames();

得到错误:

Fatal error: Using $this when not in object context

而这个(来自同一个 SO 帖子)

$q = $dbh->prepare("DESCRIBE username");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
print_r($table_fields);

产生了错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2812 General SQL Server error: Check messages from the SQL Server [2812] (severity 16) [(null)]'

我只是想获取列的名称,这样我就可以遍历并获取它们每一行的值。我怎样才能做到这一点?谢谢

【问题讨论】:

    标签: php sql-server pdo


    【解决方案1】:

    DESCRIBE 是 MySQL 特定的命令。在 MS SQL 上,您可以为此使用存储过程:

    exec sp_columns MyTable
    

    您可以在 MSDN 找到文档

    下面是一个如何使用 PDO 完成此操作的小示例:

    <?php
    
    // will contain the result value
    $return = null;
    
    // replace table name  by your table name
    $table_name = 'table_name';
    
    // prepare a statement
    $statement = $pdo->prepare("exec sp_columns @table_name = :table_name");
    
    // execute the statement with table_name as param
    $statement->execute(array(
        'table_name' => $table_name
    ));
    
    // fetch results
    $result = $statement->fetchAll($sql);
    
    // test output
    var_dump($result);
    

    【讨论】:

    • 感谢您将我指向文档。虽然我真的不明白我必须做什么。你能给我举个例子吗?
    • 不幸的是,我没有用于测试的 MS SQL 服务器。我找到了this。有帮助吗?
    • 不幸的是没有那么多。我真的不太了解准备好的陈述。现在只是想深入研究它们。感谢您的帮助
    • 会尝试举个例子..(可惜不能测试)
    【解决方案2】:

    这是一个老问题,但无论如何我都会补充一下我学到的东西。我设法通过这样做来获取列值。

    try {
                $query = $this->db->prepare("DESCRIBE posts");
                $query->execute();
            //retrieve the columns inside the table posts
                $forumrows = $query->fetchAll(PDO::FETCH_COLUMN); 
                //var_dump($forumrows);
            //Output each column Id with its Value
                foreach($forumrows as $forumrow) {
                    echo $forumrow . "</br>";
            }
              } catch(PDOException $e) {
                    echo $e->getMessage();
            }
    

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 1970-01-01
      • 2011-07-22
      • 2016-02-10
      • 2011-07-26
      • 1970-01-01
      • 2014-02-16
      • 2021-06-05
      相关资源
      最近更新 更多