如何以数组的形式检索数据
SQL Server Driver for PHP 提供了以数组的形式检索数据行的 sqlsrv_fetch_array 函数。本主题提供了将行数据作为数组检索的两个示例:
第一个示例说明了如何使用 sqlsrv_fetch_array 将行数据作为数字索引数组检索。
第二个示例说明如何使用 sqlsrv_fetch_array 将数据行作为关联数组检索。
备注
默认情况下,sqlsrv_fetch_array 将检索同时带有数字键和关联键的数组。
示例
以下示例将检索结果集的每一行作为数字索引数组。
该示例从 AdventureWorks 数据库的 Purchasing.PurchaseOrderDetail 表中检索具有指定日期且其库存数量 (StockQty) 少于指定值的产品的产品信息。
此示例假定本地计算机上已安装了 SQL Server 和 AdventureWorks 数据库。从命令行运行此示例时,所有的输出都将写入控制台。
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Define the query. */
$tsql = "SELECT ProductID,
UnitPrice,
StockedQty
FROM Purchasing.PurchaseOrderDetail
WHERE StockedQty < 3
AND DueDate='2002-01-29'";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each
iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo "ProdID: ".$row[0]."\n";
echo "UnitPrice: ".$row[1]."\n";
echo "StockedQty: ".$row[2]."\n";
echo "-----------------\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
sqlsrv_fetch_array 函数始终根据默认的 PHP 数据类型返回数据。有关如何指定 PHP 数据类型的信息,请参阅如何指定 PHP 数据类型或如何检索单个字段。
如果检索到没有名称的字段,则该数组元素的关联键将为空字符串 ("")。有关详细信息,请参阅 sqlsrv_fetch_array。
下一示例将检索结果集的每一行作为一个关联数组。数组的键与结果集的列名相对应。
该示例从 AdventureWorks 数据库的 Purchasing.PurchaseOrderDetail 表中检索库存数量 (StockQty) 少于指定值且具有指定截止日期 (DueDate) 的产品的产品信息。
此示例假定本地计算机上已安装了 SQL Server 和 AdventureWorks 数据库。从命令行运行此示例时,所有的输出都将写入控制台。
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Define the query. */
$tsql = "SELECT ProductID,
UnitPrice,
StockedQty
FROM Purchasing.PurchaseOrderDetail
WHERE StockedQty < 3
AND DueDate='2002-01-29'";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each
iteration. Note that the returned array is an associative array with
keys corresponding to column names of the result set. */
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo "ProdID: ".$row['ProductID']."\n";
echo "UnitPrice: ".$row['UnitPrice']."\n";
echo "StockedQty: ".$row['StockedQty']."\n";
echo "-----------------\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
sqlsrv_fetch_array 函数始终根据默认的 PHP 数据类型返回数据。有关如何指定 PHP 数据类型的信息,请参阅如何指定 PHP 数据类型或如何检索单个字段。