如何在SQL Server中获取列名?

时间:2010-09-04 10:03:00

标签: php sql-server

如何在SQL Server中获取列名?

还有,如何在PHP中执行该查询?

3 个答案:

答案 0 :(得分:4)

<强> 1。在SQL Server中获取列名

例如:

SELECT table_name=sysobjects.name,
       column_name=syscolumns.name,
       datatype=systypes.name,
       length=syscolumns.length
FROM sysobjects

<强> 2。在PHP中执行查询的示例

$firstname = 'fred';
$lastname  = 'fox';

$query = sprintf("SELECT firstname, lastname FROM friends WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

$result = mysql_query($query);

答案 1 :(得分:1)

以下是如何做到这一点

$result = mysql_query("DESCRIBE TABLE table");
$data = mysql_fetch_assoc($result);
print_r($data);

答案 2 :(得分:0)

我的问题已通过以下代码修复。

/*
** Connect to database:
*/   // Connect to the database (host, username, password) $con =
mssql_connect('localhost','admin','foo')
    or die('Could not connect to the server!'); // Select a database:

mssql_select_db('Northwind')
    or die('Could not select a database.'); // Example query: (TOP 10 equal LIMIT 0,10 in MySQL)
$SQL =
    "SELECT TOP 10 * FROM ExampleTable
     ORDER BY ID ASC";

// Execute query:
$result = mssql_query($SQL)
    or die('A error occured: ' . mssql_error()); // Get result count:
$Count = mssql_num_rows($result);
print "Showing $count rows:<hr/>\n\n";
// Fetch rows: while ($Row =
mssql_fetch_assoc($result)) {
    print $Row['Fieldname'] . "\n";
}
mssql_close($con);