[英]Select Column Names from SQL Server using PDO
我正在嘗試使用以下代碼從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? 我搜索了谷歌高和低沒有運氣。 我幾乎肯定這是正確的語法,而且我確信它可以正確查詢數據庫。
嘗試刪除“ 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'";
您的where語句看起來不對,它不應where table_name = dbo.myTable
它應該是
where
table_name = 'myTable'
and TABLE_SCHEMA = 'dbo'
一件事簡單:拿那個“ dbo”。 表名之外。 該值將在“ table_schema”列中找到。 因此它應該是“ ... where table_name ='myTablel'”
假設已安裝所有驅動程序,並且可以從其他查詢中獲取結果,則可以完成此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.