Hi. I know this seems a bit simple but I cannot get it to work!

I am trying to delete all records from an Oracle database using PHP via a web page. The code I'm using is;

<?php
//This page connects to Oracle 8 database and deletes
//all entries in database

$Sql = "DELETE FROM TABLE";

if ($conn = ocilogon("xxx", "xxx"))
{
$stmt = ociparse($conn,$Sql);
ociexecute($stmt,OCI_DEFAULT);
}

?>

I get no errors returned when I run this but the records are not deleted from the table. Many thanks.

    You probably should put in some sort of an else statement to let you know if the logon fails. That way you can start to narrow it down a bit.

    Also, what is the name of your table?

      Thanks for that - am a bit of a novice at PHP. Table is called 'abstracts'.

        So, are you running the statement:

        $Sql = "DELETE FROM TABLE";

        OR

        $Sql = "DELETE FROM abstracts";

          I am running the query;

          $Sql = "DELETE FROM ABSTRACTS";

          I just used TABLE as a substitute for Abstracts.

            Well, to try to figure out where it is failing I would start with something like this to see if you are getting the connection.

            $Sql = "DELETE FROM ABSTRACTS";
            $conn = ocilogon("xxx", "xxx");

            if (!$conn) {
            echo "Could not connect to database";
            } else {
            $stmt = ociparse($conn,$Sql);
            ociexecute($stmt,OCI_DEFAULT);
            }

              There are no problems with that code. It connects and runs the HTML;

              All records deleted.
              <br /><br />

              Click <a href = "index.php">here</a> to return to the index.

              When I type 'delete from abstracts;' directly into Oracle it does not seem to immediately delete all of the records. I have a feeling it might be an administrative problem because other SQL/PHP commands work OK using the same connect code ('select * from abstracts;' and printing the records out into a table works fine as does adding records to the table).

                Sounds like you just need to do a commit after you do the delete. Otherwise, Oracle waits until you end your session to actually commit the delete (or update).

                  I tried the commit and it works fine but changing OCI_DEFAULT to OCI_COMMIT_ON_SUCCESS seems to be the easiest way to solve the problem. Thanks for your help!

                    Write a Reply...