首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

sqlsrv_execute

(没有可用的版本信息,可能只在Git中)

sqlsrv_execute - 执行使用sqlsrv_prepare()准备的语句

描述

代码语言:javascript
复制
bool sqlsrv_execute ( resource $stmt )

执行使用sqlsrv_prepare()准备的语句。这个函数非常适合用不同的参数值多次执行一个准备好的语句。

参数

stmt

由sqlsrv_prepare()返回的语句资源。

返回值

成功时返回TRUE或失败时返回FALSE

例子

示例#1 sqlsrv_execute()示例

此示例演示如何使用sqlsrv_execute()使用sqlsrv_prepare()准备语句并多次重复执行它(使用不同的参数值

代码语言:javascript
复制
<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "UPDATE Table_1
        SET OrderQty = ?
        WHERE SalesOrderID = ?";

// Initialize parameters and prepare the statement. 
// Variables $qty and $id are bound to the statement, $stmt.
$qty = 0; $id = 0;
$stmt = sqlsrv_prepare( $conn, $sql, array( &$qty, &$id));
if( !$stmt ) {
    die( print_r( sqlsrv_errors(), true));
}

// Set up the SalesOrderDetailID and OrderQty information. 
// This array maps the order ID to order quantity in key=>value pairs.
$orders = array( 1=>10, 2=>20, 3=>30);

// Execute the statement for each order.
foreach( $orders as $id => $qty) {
    // Because $id and $qty are bound to $stmt1, their updated
    // values are used with each execution of the statement. 
    if( sqlsrv_execute( $stmt ) === false ) {
          die( print_r( sqlsrv_errors(), true));
    }
}
?>

笔记

当您准备一个使用变量作为参数的语句时,变量将绑定到该语句。这意味着如果您更新变量的值,则在下次执行语句时,它将使用更新的参数值运行。对于您计划只执行一次的语句,请使用sqlsrv_query()。

扫码关注腾讯云开发者

领取腾讯云代金券