我正在尝试使用以下代码从SQL Server数据库中选择列名:
public function getColumns($table){
$columns = array();
$sql = "select column_name from information_schema.columns where table_name = 'dbo.myTable'";
$stmt = $this->conn()->prepare($sql);
try {
if($stmt->execute()){
$raw_column_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
//
//this is empty so we can stop right there
//
echo "<pre>";
print_r($raw_column_data);
echo "</pre>";
}else{
echo "<pre>";
print_r($stmt->errorInfo());
echo "</pre>";
}
return $columns;
} catch (Exception $e){
return $e->getMessage(); //return exception
}
}
为什么'$ raw_column_data`数组可能为null?我搜索谷歌的高低没有运气。我几乎肯定这是正确的语法,我确信它正确地查询数据库。
答案 0 :(得分:1)
尝试删除“dbo”。在这一行...
$sql = "select column_name from information_schema.columns where table_name = 'dbo.myTable'";
更改为:
$sql = "select column_name from information_schema.columns where table_name = 'myTable'";
答案 1 :(得分:0)
您的where语句看起来有误,不应该是where table_name = dbo.myTable
应该是
where
table_name = 'myTable'
and TABLE_SCHEMA = 'dbo'
答案 2 :(得分:0)
一件简单的事:拿出那个“dbo”。超出表名。该值将在“table_schema”列中找到。所以它应该是“...... table_name ='myTablel'”
假设您安装了所有驱动程序,并且可以从其他查询中获得结果,那就可以了。