How to fetch single row from Oracle in PHP?

Prat picture Prat · Apr 19, 2012 · Viewed 10.3k times · Source

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

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...

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

plz guide me...

Answer

Frosty Z picture Frosty Z · Apr 19, 2012

Basically, you just have to remove the while loop.

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