Insert Data Using Oracle PHP


0

Here is an example of how to insert a record into an Oracle database using PHP:

Also Read:

<?php
    // Connect to the Oracle database
    $conn = oci_connect('username', 'password', 'hostname/database');

    // Prepare the SQL query
    $query = "INSERT INTO tablename (column1, column2, column3) VALUES (:bind1, :bind2, :bind3)";
    $stid = oci_parse($conn, $query);

    // Bind the values to the query
    oci_bind_by_name($stid, ':bind1', $val1);
    oci_bind_by_name($stid, ':bind2', $val2);
    oci_bind_by_name($stid, ':bind3', $val3);

    // Execute the query
    $result = oci_execute($stid);

    // Check for errors
    if (!$result) {
        $e = oci_error($stid);
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }

    // Close the connection
    oci_close($conn);
?>

In this example, username, password, and hostname/database should be replaced with the appropriate values for your Oracle database. tablename should be replaced with the name of the table you want to insert into, and column1, column2, and column3 should be replaced with the names of the columns in that table. The values you want to insert into those columns should be stored in the variables $val1, $val2, and $val3, respectively.

It is important to note that, the above example uses the deprecated oci_* functions, Oracle recommends the use of PDO_OCI extension to connect to oracle database instead of oci_* functions.

Also, it is recommended to use prepared statement to prevent SQL injection and it is more secure.


Like it? Share with your friends!

0
Developer

0 Comments