In this quick code example, I will provide code which will do the following:

 

  • Define database connection CONSTANTS
  • Connection to Oracle with a permanent connection
  • Create a SELECT statement with bind variables
  • Bind the SELECT statement variables to PHP variables
  • Execute the Oracle statement
  • Loop through the returned record set using an associated array

 

Using bind variables provides much better performance from Oracle as it can reuse an execution path.

 

<?php

// define some constants
define("DB_USER", "username");
define("DB_PASS", "password");
define("DB", "//host:1523/sid");

// connect to oracle
if(!$con = oci_pconnect(DB_USER, DB_PASS, DB)) {
  echo "Cannot connect to database.";
  exit(9);
}

// sql with bind variable
$sql = "select field_a from table_a where field_c = :somevalue_a";
$parse_sql = oci_parse($con, $sql);

$somevariable_a = "some value";

// bind the sql variable to php variable
oci_bind_by_name($parse_sql, ":somevalue_a", $somevariable_a);
oci_execute($parse_sql);

// fetch the results and loop through
while ($row = oci_fetch_assoc($parse_sql)) {
  // grab the contents of the field
  $field_a = $row['FIELD_A'];
}

?>

 

 

In another post, I provide code for an Oracle INSERT statement using bind variables.

 

 

 

Tags: , , ,