我正在从一些 MS SQL PDO 查询构建一个 HTML 表。或试图。我遇到的第一个障碍是我无法获取特定表的列名。在这里找到,我已经尝试过解决方案
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)]'
我只是想获取列的名称,这样我就可以遍历并获取它们每一行的值。我怎样才能做到这一点?谢谢