我从表中返回列名,并希望返回这些列的数据类型以定义搜索条件。我有代码,因为我希望从任何表返回任何数据。但是,INFORMATION_SCHEMA.COLUMNS中的DATA_TYPE始终显示为值1。
<?php
$column_names_sql = "SELECT CHARACTER_MAXIMUM_LENGTH, ORDINAL_POSITION, COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='HardwareAsset'";
$column_names = sqlsrv_query($database_connection, $column_names_sql);
echo "<option value=''>Select Attribute</option>";
while ($column_names_option = sqlsrv_fetch_object($column_names)) {
if ($column_names_option->ORDINAL_POSITION == 2) {
echo "<option value='$column_names_option->COLUMN_NAME'>" . preg_replace('/(?<!\ )[A-Z]/', ' $0', str_replace(['HardwareAsset', 'ID'], ' ', $column_names_option->COLUMN_NAME)) . "</option>";
}
if (($column_names_option->ORDINAL_POSITION !== 1 && $column_names_option->ORDINAL_POSITION !== 2) && ($column_names_option->DATA_TYPE = 'varchar' && $column_names_option->CHARACTER_MAXIMUM_LENGTH < '256')) {
echo "<option value='$column_names_option->COLUMN_NAME' type='$column_names_option->DATA_TYPE'>" . preg_replace('/(?<!\ )[A-Z]/', ' $0', str_replace(['HardwareAsset', 'ID'], ' ', $column_names_option->COLUMN_NAME)) . "</option>";
}
}
?>
这是select返回的内容:
<option value="HardwareAssetAssetTag" type="1"> Asset Tag</option>