sqlsrv_execute

PHP ドライバーのダウンロード

事前に準備されたステートメントを実行します。 実行するステートメントの準備の詳細については、「 sqlsrv_prepare 」を参照してください。

Note

この関数は、さまざまなパラメーター値を使用して事前に準備したステートメントを複数回実行する場合に適しています。

構文

  
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