如何在不使用PDO的情况下绑定sqlsrv中的参数

时间:2016-06-29 04:37:52

标签: php sqlsrv

在MySQL中,我们使用mysqli_stmt_bind_param来绑定参数。 我应该用什么来绑定sqlsrv中的参数?

$sql = "SELECT * FROM dbo.[user] WHERE username = ? and password = ?";
$stmt = sqlsrv_prepare($conn, $sql, $params);
if($stmt === false){
die( print_r( sqlsrv_errors(), true));
}

如何绑定此参数?这是一个php文件,我需要在没有pdo的情况下绑定它们。

1 个答案:

答案 0 :(得分:1)

您没有使用其他函数显式绑定参数,而是在准备语句时执行此操作。

请参阅manual中的示例。

$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));
    }
}