By Sergey Skudaev
Before reading this page visit PHP: Migrating from MySQL to Oracle, which is the first part of my tutorial about using Oracle database with PHP. Here is an insert code example. First, you have to create connection.
To connect to Oracle database we use the following PHP code:
$conn=oci_connect($dbuser, $dbpassword, $dbhost);
Check if connection is created:
if (!$conn) { $e = oci_error(); // For oci_connect errors pass no handle echo "if not connection<br>"; echo htmlentities($e['message']); } else { $isql="INSERT INTO usernames VALUES(usernames_seq.nextval, 'Smith', 'James', 'secret', 'jamess@yahoo.com', 'admin', '29-JAN-01', '12 west 15 STREET', 'Dunedin', 'FL', '34697'); $stmt = oci_parse($conn,$isql); $rc=oci_execute($stmt); if(!$rc) { $e=oci_error($stmt); var_dump($e); } oci_commit($conn); oci_free_statement($stmt); oci_close($conn);
It is considered as more efficient way to insert data in Oracle table by binding variable to Oracle placeholder. The data associated with a bind variable is never treated as part of the SQL statement and as a result it reduces possibility of SQL Injection. Besides, it increases performance
Do not use magic_quotes_gpc or addslashes() when you use oci_bind_by_name() as no quoting is needed. "oci_bind_by_name()" inserts data without removing quotes or escape characters.
$isql="insert into(userid, lastname, firstname, username, password, email, role, dob, address, city, state, zip) values (:userid_bv, :lastname_bv, :firstname_bv, :username_bv, :password_bv, :email_bv, :role_bv, :dob_bv, :address_bv, :city_bv, :state_bv, :zip_bv)"; $stmt = oci_parse($conn,$isql); $userid=7; $lastname='Smith'; $firstname='John'; $username='johns; $password='password'; $email='johns@yahoo.com'; $role='user'; $dob='10-MAR-09'; $address='12 East 19 street'; $city='New York'; $state='NY'; $zip='11200'; oci_bind_by_name($stmt, ":userid", $userid oci_bind_by_name($stmt, ":lastname_bv", $lastname); oci_bind_by_name($stmt, ":firstname_bv", $firstname); oci_bind_by_name($stmt, "username_bv", $username); oci_bind_by_name($stmt, ":password_bv", $password); oci_bind_by_name($stmt, ":email_bv", $email); oci_bind_by_name($stmt, ":role_bv", $role); oci_bind_by_name($stmt, ":dob_bv", $dob); oci_bind_by_name($stmt, ":address_bv",$address); oci_bind_by_name($stmt, ":city_bv", $city); oci_bind_by_name($stmt, ":state_bv",$state); oci_bind_by_name($stmt, ":zip_bv",$zip); oci_execute($stmt); oci_free_statement($stmt); oci_close($conn);
To update a record in Oracle table with PHP you should use binding PHP variable to Oracle placeholder as it is more efficient and safe way. This is an example of updating a user email address
$usql="update usernames set email=:email_bv where username=:username_bv"; $stmt = oci_parse($conn,$usql); $username='johns; $email='johns@gmail.com'; oci_bind_by_name($stmt, ":username_bv", $username); oci_bind_by_name($stmt, ":email_bv", $email); oci_execute($stmt);
Our dog needs urgent surgery, and the cost is overwhelming.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!
Oscar Story.
Oscar wasn’t just any puppy—he was a gift from a mother who trusted us with her smallest one.
For five years, my wife worked at the Indian Medical Center in Arizona, deep in Navajo Nation. Near her clinic, she often saw a homeless dog wandering the area. Over time, she began feeding her, and the dog grew fond of her. Then, one day, that same dog brought her newborn puppies to my wife—as if proudly showing them off.
Among them was the smallest, most delicate pup. My wife couldn’t resist. She brought him home, and we named him Oscar.
Oscar thrived in the house provided by the medical center, enjoying the big backyard where he lived. I built him a sturdy wooden doghouse, and we often took him on walks along the Window Rock Trail. He became our adventure companion, making the vast desert feel like home.
After my wife’s contract ended, we moved back to Florida, bringing Oscar with us. He adjusted to his new surroundings, but he never lost his adventurous spirit.
Now, Oscar faces a tough challenge—he needs urgent surgery, and the cost is overwhelming. We want to give him the best care possible, just as he’s given us years of joy and loyalty.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!