Ronald
Ronald

Reputation: 557

Error Handling PHP Oracle Database

I am trying to throw implement some error handling using Oracle and PHP. If I try to insert the statement into the DB table where the PK already exists, there is no INSERT performed - yet it returns that the data was added - when really it isnt - Need some help with the Error HAndling

require('connection.inc');
require('connection_db.inc');
$conn = db_connect();

$sql = oci_parse($conn,"INSERT INTO Schema.TableA (DOE, Trips) VALUES (:doe, :trp)");

oci_bind_by_name($sql, ':doe', $f1);
oci_bind_by_name($sql, ':trp', $f2);

oci_execute($sql);

<?
if($sql)
{
    echo("Input data has been added<br><br>");
    echo("<a href='link1.php'>View Links</a>");
}
else
{
    echo("Input data has failed");
    echo "</div>";
}

?>

Upvotes: 1

Views: 5762

Answers (1)

ChrisR
ChrisR

Reputation: 14467

You are evaluating the statement identifier $sql and not the result of the oci_execute call ...

oci_execute will return true if successful and false in case the query failed. See http://php.net/manual/en/function.oci-execute.php

$conn = oci_connect('hr', 'welcome', 'localhost/XE');

$stid = oci_parse($conn, 'SELECT * FROM employees');
$result = oci_execute($stid);

if(true === $result){
    // Query successfully executed
    echo "Hooary";
} else {
    // Something went wrong
    $e = oci_error($stid);
    echo "Error: " . $e['message'];
}

Small tip, judging from the code you posted you seem to be in the process of learning php, i'd say take a look at PDO if you want to have a safer and easier way of interacting with your database. There is an oci driver available for PDO.

http://php.net/manual/en/book.pdo.php
http://php.net/manual/en/ref.pdo-oci.php

Upvotes: 2

Related Questions