检索活动结果集中的字段数。请注意,可以在执行任何预定义语句之前或之后对其调用 sqlsrv_num_fields。
sqlsrv_num_fields( resource $stmt)
$stmt:目标结果集在其上处于活动状态的语句。
表示活动结果集中的字段数的整数值。如果发生错误,则返回布尔值 false。
以下示例执行查询以检索 Adventureworks 数据库的 HumanResources.Department 表中前三行的所有字段。sqlsrv_num_fields 函数确定相应结果集中的字段数。这样可以通过循环访问每个返回行中的字段来显示数据。
此示例假定本地计算机上已安装 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 and execute the query. */ $tsql = "SELECT TOP (3) * FROM HumanResources.Department"; $stmt = sqlsrv_query($conn, $tsql); if( $stmt === false) { echo "Error in executing query.\n"; die( print_r( sqlsrv_errors(), true)); } /* Retrieve the number of fields. */ $numFields = sqlsrv_num_fields( $stmt ); /* Iterate through each row of the result set. */ while( sqlsrv_fetch( $stmt )) { /* Iterate through the fields of each row. */ for($i = 0; $i < $numFields; $i++) { echo sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))." "; } echo "\n"; } /* Free statement and connection resources. */ sqlsrv_free_stmt( $stmt ); sqlsrv_close( $conn ); ?>
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有