oci_pdo_empT1S.htm
This document supplements  to oci_pdo_empT1.htm.
Objectives:
  • Connection like using TNSNAMES.ORA
  • new PDO connection:
    • $conn = new PDO("oci:dbname=".$tns,$db_username,$db_password);
      $stmt = $conn->prepare($sql);

      $stmt->execute();
      echo "<br/>select query worked";
      $count = $stmt->rowCount();// did not work
      used a variable "$count", in for loop which traverse through "x" number of rows,
  • Use of PDO:FETCH: $stmt->fetch(PDO::FETCH_OBJ)

 

 

PHP Code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>oci_pdo_empT1S.php</title>
</head>
oci_pdo_empT2.php
<body>
<?php
// oci_pdo_empT2.php
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl.gateway.2wire.net)
)
)
";
$db_username = "scott";
$db_password = "Son";

try{
$conn = new PDO("oci:dbname=".$tns,$db_username,$db_password);
echo "Hurrah HO!!! Oacle pdo extension is working";

/*** SQL SELECT statement ***/
$sql = "SELECT e.eid,e.info.fname,e.info.lname, e.info.address from EMPT1 e";
$count="";
// uses pdo and object table
/*** fetch into an PDOStatement object ***/
$stmt = $conn->prepare($sql);
$stmt->execute();
//$count = $stmt->rowCount();// DOES NOT WORK
$count = "";
echo "<br/>select query worked";
/*** loop over the object directly ***/
echo "<table border='1'>\n";
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$count++;
echo "<tr>\n";
foreach ($row as $item) {
echo "<td class='td2'>".($item !== null ? htmlentities($item, ENT_QUOTES):"&nbsp;")."</td>\n";
} //foreach ends

}
//
echo "</table>\n";
//
//echo " no of rows : ". oci_num_rows($stmt);
echo "no.of rows :".$count;

/*** close the database connection ***/
$conn= null;
//
}catch(PDOException $e){
echo ($e->getMessage());
}
echo "bye";
?>
</body>
&nbsp;</html>