FYI_PHP_PDO1.htm |
PHP/PDO: Oracle Extensions
Reference :
http://php.net/manual/en/ref.pdo-oci.php
|
PDO Extension : (At the time preparing this document, this was
released as an EXPERIMENTAL PHP extension to use OCI library of
Oracle database).PHP Data Objects (PDO) is a data abstraction extension,
ships with php 5.1 and later,provides PHP
functions for accessing databases using a common core of database
independent methods. Each database has its own driver, which may also
support vendor specific functionality. PDO_OCI provides the Oracle
functionality for PDO. The PDO extension and PDO_OCI driver are open
source and included in PHP 5.1 onwards.
- pdo::prepare //execute : Prepares a statement for
execution (not immediate execution) and returns a statement
object . PDO::prepae, offers immunity to sql injection
(malicious query input via web input )
- pdo::query: executes an SQL statement in a single function
call
- $conn = new PDO("oci:dbname=".$tns,$db_username,$db_password);
$stmt=$conn->query($sql); // connecting db with statement
while ($row = $conn->fetch(PDO::FETCH_ASSOC)) {
print "<p>Name: {$row[0] $row[1]}</p>";
}
- $data = $conn->query($SQL. $conn->quote($name));
foreach($data as $row) {
print_r($row);
}
pdo::quote : quotes required for sql statement
|
OCI 8 Module :
|
Oracle
A Screenshot from Oracle table, "EMPT1"
PHP: PDO code :Oracle Table EMPT1
<?php
// oci_pdo_empT1.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";
/*** The SQL SELECT statement ***/
$sql = "SELECT e.eid,e.info.fname,e.info.lname, e.info.address from
EMPT1 e";
// uses pdo and object table
/*** fetch into an PDOStatement object ***/
$stmt = $conn->prepare($sql);
$stmt->execute();
/*** loop over the object directly ***/
echo "<table border='1'>\n";
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
echo "<tr>\n";
foreach ($row as $item) {
echo "<td class='td2'>".($item !== null ? htmlentities($item,
ENT_QUOTES):" ")."</td>\n";
} //foreach ends
}
//
echo "</table>\n";
/*** close the database connection ***/
$conn= null;
//
}catch(PDOException $e){
echo ($e->getMessage());
}
echo "bye";
?>
</body>
</html>
|
PostgreSQL:
<?php
$hostname = 'localhost';
$username = 'manas237';
$password = 'pwmanas237';
try {
//$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
$dbconn= new PDO("pgsql:host=$hostname;dbname=pgsdemo1", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM emp1 ORDER BY fname";
echo "sql passed <br/>";
/*** fetch into an PDOStatement object ***/
$stmt = $dbconn->query($sql);
echo "PDOStatement object... created <br/>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['fname'].' '.$row['lname'].' '.$row['address'].' '.$row['manager'];
echo("<br/>");
}
/** close while ***/
/*** close the database connection ***/
$dbconn= null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
|
Screen shot of a table, "emp1"
MySQL
<?php
//test_pdo_mysql1.php
echo php_uname();
//mysql_connect("Manas9", "Manas9", "Manas9237");
//$con = new PDO("Manas9", "Manas9", "Manas9237");
$dsn = 'mysql:dbname=test;host=Manas9';
$user = 'Manas9';
$password = 'Manas9237';
$conn='';
try {
$conn = new PDO($dsn, $user, $password);
echo" <br/>Passing through try block";
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM emp1";
// uses pdo and object table
/*** fetch into an PDOStatement object ***/
$stmt = $conn->prepare($sql);
$stmt->execute();
/*** loop over the object directly ***/
echo "<table border='1'>\n";
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
echo "<tr>\n";
foreach ($row as $item) {
echo "<td class='td2'>".($item !== null ? htmlentities($item,
ENT_QUOTES):" ")."</td>\n";
} //foreach ends
}
//
echo "</table>\n";
/*** close the database connection ***/
$conn= null;
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
// some code
echo "<br/> Database : connected";
?>
|
|