PHP-mssql_fetch_array
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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
?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 ofmssql_query
inmssql_fetch_array
难道日期应该被转义吗?
代码:
$query 可能如下所示:
could it be that the date should be escaped?
Code:
$query may look like:
我通过将 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.
while($row = mssql_fetch_array( $items ))
之前的最后一个查询是INSERT
,因此mssql_fetch_array()
永远不会获取任何内容!The last query before
while($row = mssql_fetch_array( $items ))
is anINSERT
, somssql_fetch_array()
will never fetch anything!