PHP-mssql_fetch_array

发布于 2024-12-28 07:40:06 字数 1185 浏览 1 评论 0原文

我正在尝试通过 PHP 更新 MSSQL 数据库,如以下代码所示。问题是前 5 行代码已成功执行,但程序没有进入 while 循环。我确信数组 $items 包含记录。

function updateOrder($items, $cardNo){
    if($items){
        $orderid = generateGuid();
        $username = $_SESSION['username'];

        $query = "INSERT INTO Orders (OrderId, OrderDate, OrderStatus, OrderCardNo, OrderDiscount, OrderVatRate, CustomerUsername) ";
        $query .= " VALUES ('".mssql_guid_string($orderid)."',".date('Y-m-d').", 'PE', '$cardNo', 0, 0.18, '$username')";
        $result = mssql_query($query) or die("Unable to place order"/*mssql_get_last_message()*/);


        while($row = mssql_fetch_array( $items )){
            $tmpId = generateGuid();
            $tmpPrice = getUserPrice($username, $row["ProductId"]);

            $query = "INSERT INTO Orders_Details (OrderDetailsId, ProductPrice, Qty, OrderId, ProductId)";
            $query .= "VALUES ('".mssql_guid_string($tmpId)."', $tmpPrice, '".$row["Qty"]."', '".mssql_guid_string($orderid)."', 0, 0.18, '".$row["ProductId"]."')";

            echo($query);
            $result = mssql_query($query) or die("Unable to place order"/*mssql_get_last_message()*/);
        }
    }
}

I am trying to update an MSSQL database through PHP, as shown in in the following code. The problem is that the first 5 lines of code are successfully being executed but the program is not entering in the while loop. I am sure that the array $items contains records.

function updateOrder($items, $cardNo){
    if($items){
        $orderid = generateGuid();
        $username = $_SESSION['username'];

        $query = "INSERT INTO Orders (OrderId, OrderDate, OrderStatus, OrderCardNo, OrderDiscount, OrderVatRate, CustomerUsername) ";
        $query .= " VALUES ('".mssql_guid_string($orderid)."',".date('Y-m-d').", 'PE', '$cardNo', 0, 0.18, '$username')";
        $result = mssql_query($query) or die("Unable to place order"/*mssql_get_last_message()*/);


        while($row = mssql_fetch_array( $items )){
            $tmpId = generateGuid();
            $tmpPrice = getUserPrice($username, $row["ProductId"]);

            $query = "INSERT INTO Orders_Details (OrderDetailsId, ProductPrice, Qty, OrderId, ProductId)";
            $query .= "VALUES ('".mssql_guid_string($tmpId)."', $tmpPrice, '".$row["Qty"]."', '".mssql_guid_string($orderid)."', 0, 0.18, '".$row["ProductId"]."')";

            echo($query);
            $result = mssql_query($query) or die("Unable to place order"/*mssql_get_last_message()*/);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

铜锣湾横着走 2025-01-04 07:40:06

mssql_fetch_array 采用一个 mssql_query 返回的参数: http://php .net/manual/en/function.mssql-fetch-array.php$items 是什么?

mssql_fetch_array takes a parameter that is the return of a mssql_query : http://php.net/manual/en/function.mssql-fetch-array.php. What is $items?

纵性 2025-01-04 07:40:06

while($row = mssql_fetch_array( $items )) 中的 $items 出现问题,要么它没有值,要么您在 $items 中发送错误

如果项目具有任何值,例如items id 然后运行选择查询来选择 $items 中具有 id 的项目,并在 mssql_fetch_array 中使用 mssql_query 的返回

Something worng with $items in while($row = mssql_fetch_array( $items )) either it's having no value or you are sending wrong in $items

If items having any value like items id then run a select query for selecting items having id in $items and use return of mssql_query in mssql_fetch_array

无所谓啦 2025-01-04 07:40:06

难道日期应该被转义吗?

代码:

 $query = "INSERT INTO Orders (OrderId, OrderDate, OrderStatus, OrderCardNo, OrderDiscount, OrderVatRate, CustomerUsername) ";
 $query .= " VALUES ('".mssql_guid_string($orderid)."',".date('Y-m-d').", 'PE', '$cardNo', 0, 0.18, '$username')";

$query 可能如下所示:

INSERT INTO Orders (OrderId, OrderDate, OrderStatus, OrderCardNo, OrderDiscount,
   OrderVatRate, CustomerUsername) 
   VALUES ('100',2012-1-20,'PE','10', 0, 0.18, 'username')

could it be that the date should be escaped?

Code:

 $query = "INSERT INTO Orders (OrderId, OrderDate, OrderStatus, OrderCardNo, OrderDiscount, OrderVatRate, CustomerUsername) ";
 $query .= " VALUES ('".mssql_guid_string($orderid)."',".date('Y-m-d').", 'PE', '$cardNo', 0, 0.18, '$username')";

$query may look like:

INSERT INTO Orders (OrderId, OrderDate, OrderStatus, OrderCardNo, OrderDiscount,
   OrderVatRate, CustomerUsername) 
   VALUES ('100',2012-1-20,'PE','10', 0, 0.18, 'username')
魂牵梦绕锁你心扉 2025-01-04 07:40:06

我通过将 2 个 UPDATE 操作分成两个不同的方法并调用两次填充 $items 的方法来解决这个问题。

I solved the problem by separating the 2 UPDATE actions into two different methods and call the method that populates $items twice.

美胚控场 2025-01-04 07:40:06

while($row = mssql_fetch_array( $items )) 之前的最后一个查询是 INSERT,因此 mssql_fetch_array() 永远不会获取任何内容!

The last query before while($row = mssql_fetch_array( $items )) is an INSERT, so mssql_fetch_array() will never fetch anything!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文