隐藏

sqlsrv_execute

发布:2014/7/8 23:56:09作者:管理员 来源:本站 浏览次数:1514

执行先前准备的语句。有关为执行准备语句的信息,请参阅 sqlsrv_prepare

Cc296162.note(zh-cn,SQL.90).gif注意:
当多次执行预定义语句并为每次执行使用不同的参数值时,此函数非常理想。有关详细信息,请参阅如何多次执行一个查询



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);
?>