微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql使用准备好的语句更新foreach循环中的多列

如何解决sql使用准备好的语句更新foreach循环中的多列

我正在研究有关如何准备多次执行 UPDATE 语句的 PHP 脚本。下面的脚本显示了使用准备好的语句更新 1 列。

PHP 手册中的示例 https://www.php.net/manual/en/function.sqlsrv-prepare.php

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

如果我有多个列要更新怎么办,如何创建一个数组来将多个变量绑定到 foreach 中的准备好的语句?

新的 3 列更新 sql 语句。

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

解决方法

您可以尝试使用不同的实际参数值构建数组。并修正 UPDATE 语句的语法:

<?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 = ?,ProductName = ?,ProductPrice = ?
    WHERE SalesOrderID = ?
";

// Initialize parameters and prepare the statement. 
// Variables $qty and $id are bound to the statement,$stmt.
$qty = 0; $name = ""; $price = 0.00; $id = 0;
$stmt = sqlsrv_prepare($conn,$sql,array(&$qty,&$name,&$price,&$id));
if ($stmt === false) {
    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(
    array("qty" => 10,"name" => "Product1","price" => 10.01,"id" => 1),array("qty" => 20,"name" => "Product2","price" => 10.02,"id" => 2),array("qty" => 30,"name" => "Product3","price" => 10.03,"id" => 3)
);

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

// End
sqlsrv_free_stmt($stmt);  
sqlsrv_close($conn); 
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。