Getting Error Message For oci_execute() Error (PHP

2020-06-26 08:33发布

问题:

I would like to get the specific error message if a query fails in Oracle 10g. For MySQL, PHP has the mysql_error() function that can return details about why a query failed. I check the php.net manual for the oci_execute() function, but from what I see it only returns false on fail.

I tried using oc_error(), but I am not getting anything from it.

Here is a code sample:

    $err = array();
    $e = 0;

    //Cycle through all files and insert new records into database
    for($f=0; $f<sizeof($files); $f++)
    {
        $invoice_number = $files[$f]['invoice_number'];
        $sold_to = $files[$f]['sold_to'];
        $date = $files[$f]['date'];

        $sql = "insert into invoice (dealer_id, invoice_number, invoice_date) 
                values ('$sold_to', '$invoice_number', '$date')";

        $stid = oci_parse($conn, $sql);
        $result = oci_execute($stid);

        //If query fails
        if(!$result)
        {
            $err[$e] = oci_error();
            $e++;
        }
    } 

    print_r($err);

Response for print_r($err):

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => ) 

回答1:

Have you tried to pass $stid to oci_error?

$err[$e] = oci_error($stid);


回答2:

The oci_error() function takes an argument that specifies the context of your error. Passing it no argument will only work for certain kinds of connection errors. What you want to do is pass the parsed statement resource, like so:

$err = oci_error($stid);

(Note also that the return value from oci_error is an array, so I assigned the entire output to your $err array variable)