BC remote Oracle DBA - Call (800) 766-1884  
Oracle Consulting Oracle Training Development

Remote DBA

Remote DBA Plans  

Remote DBA Service

Remote DBA RAC

   
Remote DBA Oracle Home
Remote DBA Oracle Training
Remote DBA SQL Tuning Consulting
Remote DBA Oracle Tuning Consulting
Remote DBA Data Warehouse Consulting
Remote DBA Oracle Project Management
Remote DBA Oracle Security Assessment
Remote DBA Unix Consulting
Burleson Books
Burleson Articles
Burleson Web Courses
Burleson Qualifications
Oracle Links
Remote DBA Oracle Monitoring
Remote DBA Support Benefits
Remote DBA Plans & Prices
Our Automation Strategy
What We Monitor
Oracle Apps Support
Print Our Brochure
Contact Us (e-mail)
Oracle Job Opportunities
Oracle Consulting Prices





   

 

 

 

Remote DBA services

Remote DBA Support

Remote DBA RAC

Remote DBA Reasons

Remote Oracle Tuning

Remote DBA Links

Oracle DBA Support

Oracle DBA Forum

Oracle Disaster

Oracle Training

Oracle Tuning

Oracle Training

 Remote DBA SQL Server

Remote MSSQL Consulting

Oracle DBA Hosting

Oracle License Negotiation

 

 


 

 

 

 

 
 

Connecting to Oracle with PHP


Oracle Tips by Burleson Consulting

To connect to Oracle, PEAR DB uses DSN (Data Source Name), inspired by ODBC and JDBC.  DSN contains all the information needed to connect to a database. In particular, it contains the “driver type” such as OCI8, MySQL, PostgresSQL, MS-Access or SQL Server. It also contains a username, password, database, hostname, port and a “database identifier”, depending on the database type. PEAR DB can be used not only to connect to Oracle, but to all other database types that PHP supports. DSN follows the URL-like syntax as seen in so many JDBC applications. When connecting to an Oracle RDBMS, DSN looks like the following:

    $DSN="oci8://scott:tiger@local";

The driver type is OCI8. The username and password are the standard Oracle DEMO username and password, “scott/tiger” and the LOCAL username and password is the TNS descriptor in the database, defined as follows:

LOCAL =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = 10g))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = oracle)
      (SERVER=DEDICATED)
    )
  )

 

TNS descriptors can be created by the standard Oracle tools such as NETCA (NETwork Configuration Assistant) or NETMGR. (Net Manager). Use of these tools and an Oracle*Net configuration are beyond the scope of this book and is described in detail in the Oracle documentation.

DSN can be an associative array with the following elements:

            $DSN=array('phptype'=> 'oci8',
                               'username'=>$_POST['user'],
                               'password'=>$_POST['passwd'],
                               'database'=>$_POST['database']);

Using the array form of DSN is useful when there is a need to check each item separately; defining, for instance, the default database such as the following: 

    if  (empty $_POST['database']) {
        $_POST['database']=$_ENV['TWO_TASK'];
    }

 
Once DSN is created, all that remains to do is to call the connect method on this DSN and check for errors. The procedure to perform this task with Oracle is shown in Example 20.   

It is recommended to use the oci_new_connect()function for OCI8 in order to avoid handle initialization errors. PEAR is written in PHP4, and is easily determined by observing the ubiquitous “var” in the class definitions. If using PHP5, a small part of PEAR DB needs to be hacked manually.  On the basic PHP directory that contains files such as PEAR.php and DB.php, there is a subdirectory DB containing a file called oci8.php. On line 117, this file contains the following line:

$connect_function = $persistent ? 'OCIPlogon' : 'OCILogon';

This line MUST BE replaced by the line below:

 $connect_function = $persistent ? 'oci_pconnect' :
'oci_new_connect';

DB::connect does not work properly with PHP5 unless this line is replaced as shown above.  If this is not completed, OCI frequently issues a message that the database handle is not being properly initialized.

Documentation for each PEAR module is available on the PEAR site located at: http://pear.php.net.  Creating DSN for other databases is documented in the PEAR DB documentation.

Example 20

#!/usr/local/bin/php
<?php
require_once('DB.php');
$DSN="oci8://scott:tiger@local";
$db=DB::connect($DSN);
if (DB::iserror($db)) {
   die($db->getUserInfo(
);
SEE CODE DEPOT FOR FULL SCRIPTS
}
$sql="select * from emp";
$sth=$db->query($sql);if (DB::iserror($sth)) {
   die($sth->getUserInfo()
);
}
$cols=$db->tableInfo($sth);
echo "This result has ".count($cols)." columns\n";
foreach ($cols as $col) {
    echo $col['name']."\t";
}
echo "\n";
while ($sth->fetchinto($row)) {
      foreach ($row as $val) {
              echo "$val\t";
      }
      echo "\n";
}     
?>

The output of this example, when executed, appears as shown below:

It should be noted that the “require_once('DB.php');” line in Example 20 includes the PEAR DB module. If this line is omitted, it is not possible to work with the PEAR DB module.

PEAR is an open source module and users are welcome to study the code. Database connection, SQL handles, error objects and all other object types in PEAR DB are defined as classes.
 


The above book excerpt is from  "Easy Oracle PHP: Creating Dynamic Web Pages with Oracle Data". 

You can buy it direct from the publisher for 50%-off and get instant access to the code depot of Oracle tuning scripts:


Expert Remote DBA

BC is America's oldest and largest Remote DBA Oracle support provider.  Get real Remote DBA experts, call
BC Remote DBA today.

 

 

Remote DBA Service
 

Oracle Tuning Book

 

Advance SQL Tuning Book 

BC Oracle support

Oracle books by Rampant

Oracle monitoring software

 

 

 

 

 

 

BC Remote Oracle Support

Remote DBA

Remote DBA Services

Copyright © 1996 -  2013 by Burleson. All rights reserved.

Oracle® is the registered trademark of Oracle Corporation.



Hit Counter