Tips and scripts for checking PHP5 OCI8/PDO_OCI + Oracle 11gR1 installation

In case of Oracle 11gR1, database name is no longer such simple as SID, but need to change into something call "Global Database Name" in "name.domain" format. So the testing script for OCI8 and PDO_OCI need to have some change, too.

Let's say we have a host call "localhost.localdoman" with a database SID as "AL32UTF8", its global database name should be "AL32UTF8.localdomain". In case of checking OCI8:

<?php
  $db_conn
= ocilogon("HR", "CHANGE", "//localhost.localdomain/AL32UTF8.localdomain");
 
$cmdstr = 'SELECT "LAST_NAME", "SALARY" FROM "HR"."EMPLOYEES"';
 
$parsed = ociparse($db_conn, $cmdstr);
 
ociexecute($parsed);
 
$nrows = ocifetchstatement($parsed, $results);
  echo
"<html><head><title>Oracle PHP Test</title></head><body>";
  echo
"<center><h2>Oracle PHP Test</h2><br>";
  echo
"<table border=1 cellspacing='0' width='50%'>\n<tr>\n";
  echo
"<td><b>Name</b></td>\n<td><b>Salary</b></td>\n</tr>\n";
  for (
$i = 0; $i < $nrows; $i++ ) {
    echo
"<tr>\n";
    echo
"<td>" . $results["LAST_NAME"][$i] . "</td>";
    echo
"<td>$ " . number_format($results["SALARY"][$i], 2). "</td>";
    echo
"</tr>\n";
  }
  echo
"<tr><td colspan='2'> Number of Rows: $nrows</td></tr></table><br>";
  echo
"<em>If you see data, then it works!</em><br>";
  echo
"</center></body></html>\n";
?>

In case of checking PDO_OCI:
<?php
  $db_conn
= new PDO('oci:dbname=//localhost.localdomain/AL32UTF8.localdomain', 'HR', 'CHANGE');
 
$cmdstr = 'SELECT "LAST_NAME", "SALARY" FROM "HR"."EMPLOYEES"';
 
$stmt = $db_conn->query($cmdstr);
 
$results = $stmt->fetchAll();
 
$stmt = $db_conn->query("SELECT COUNT(*) AS nrows FROM ($cmdstr)");
 
$nrows = $stmt->fetch(PDO::FETCH_OBJ)->NROWS;
  echo
"<html><head><title>Oracle PHP Test</title></head><body>";
  echo
"<center><h2>Oracle PHP Test</h2><br>";
  echo
"<table border=1 cellspacing='0' width='50%'>\n<tr>\n";
  echo
"<td><b>Name</b></td>\n<td><b>Salary</b></td>\n</tr>\n";
  for (
$i = 0; $i < $nrows; $i++ ) {
    echo
"<tr>\n";
    echo
"<td>" . $results[$i]["LAST_NAME"] . "</td>";
    echo
"<td>$ " . number_format($results[$i]["SALARY"], 2). "</td>";
    echo
"</tr>\n";
  }
  echo
"<tr><td colspan='2'> Number of Rows: $nrows</td></tr></table><br>";
  echo
"<em>If you see data, then it works!</em><br>";
  echo
"</center></body></html>\n";
?>


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <code> <del> <blockquote> <q> <sub> <p> <br> <ul> <ol> <li> <dl> <dt> <dd> <a> <b> <u> <i> <sup> <acronym> <pre> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.