Share via


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)
設計考量