SQL Server Driver for PHP 1.0 版和 1.1 版
sqlsrv_execute
执行先前准备的语句。有关为执行准备语句的信息,请参阅 sqlsrv_prepare。
注意: |
|---|
| 当多次执行预定义语句并为每次执行使用不同的参数值时,此函数非常理想。有关详细信息,请参阅如何多次执行一个查询。 |
语法
sqlsrv_execute( resource $stmt)
参数
$stmt:指定要执行的语句的资源。有关如何创建语句资源的详细信息,请参阅 sqlsrv_prepare。
返回值
如果语句成功执行,则为布尔值 true。否则为 false。
示例
以下示例执行用于更新 AdventureWorks 数据库中 Sales.SalesOrderDetail 表中的某字段的语句。此示例假定本地计算机上已安装 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));
}
/* Set up the Transact-SQL query. */
$tsql = "UPDATE Sales.SalesOrderDetail
SET OrderQty = ( ?)
WHERE SalesOrderDetailID = ( ?)";
/* Set up the parameters array. Parameters correspond, in order, to
question marks in $tsql. */
$params = array( 5, 10);
/* Create the statement. */
$stmt = sqlsrv_prepare( $conn, $tsql, $params);
if( $stmt )
{
echo "Statement prepared.\n";
}
else
{
echo "Error in preparing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt))
{
echo "Statement executed.\n";
}
else
{
echo "Error in executing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
另请参见
参考
sqlsrv_query概念
关于文档中的代码示例其他资源
API 参考 (SQL Server Driver for PHP)设计注意事项

注意: