sqlsrv_execute

下載 PHP 驅動程式

執行先前已備妥的陳述式。 如需關於準備執行陳述式的詳細資訊,請參閱 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 驅動程式 API 參考

關於文件中的程式碼範例

sqlsrv_query