如何在 PHP 中从 Oracle 获取单行?

2022-01-20 00:00:00 row fetch php oracle oracle-call-interface

我想知道如何在 PHP 中从 Oracle 中获取单行数据?

I want to know how to fetch single row from Oracle in PHP?

检查我的脚本-:我想从 ITEM_INFO 表中获取单行将该值与变量 $sku &$code...我应用的逻辑适用于 Mysql 但不适用于 Oracle...

Chedck my script-: I want to fetch single row from ITEM_INFO table & compare that values with variables $sku & $code...Logic I applied which works in Mysql but not working in Oracle...

每次 $sku &$code 包含差异.值,所以我只需要将它们与 ITEM_INFO 表 &如果匹配,则更新相同的标志...

Each time $sku & $code contains diff. values so I just need to compare them with ITEM_INFO table & if it's matches then update the flag for the same...

$query_fetch = "SELECT ITEM_NAME,SITE_CODE FROM app.ITEM_INFO WHERE ITEM_FLAG = 'N'";
$stmt = oci_parse($conn,$query_fetch);
oci_execute($stmt);
while(($row = oci_fetch_array($stmt, OCI_BOTH))) 
{   
    $ITEM_NAME = ($row["ITEM_NAME"]);                       
    $SITE_CODE = ($row["SITE_CODE"]);   

    if(($ITEM_NAME === $sku) && ($SITE_CODE === $code))
    {   
              $query_ora_update = "UPDATE app.ITEM_INFO SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_NAME = '$sku' AND SITE_CODE = '$code' AND ITEM_FLAG = 'N' ";
                                            $parse_result = oci_parse($conn,$query_ora_update);
    $result = oci_execute($parse_result);
    oci_commit($conn);
    oci_close($conn);
    }
 }

请指导我...

推荐答案

基本上,你只需要去掉 while 循环.

Basically, you just have to remove the while loop.

这是应用该更改对代码的重写(+ 使用了太多括号,降低了代码的可读性 + 您应该使用 SQL 绑定来避免注入):

Here's a rewrite of your code applying that change (+ you use too many parenthesis, decreasing your code readability + you should use SQL binding to avoid injection):

$query_ora_update = "UPDATE app.ITEM_INFO SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_FLAG = 'N'";
$parse_result = oci_parse($conn, $query_ora_update);
$result = oci_execute($parse_result);

oci_commit($conn);
oci_close($conn);

相关文章