Written by Deepak Vohra PHP is the most commonly used scripting language on the web. Several companies including IBM, Amazon, Google, eBay, Zynga, Yahoo, Intel, Zend and Oracle use PHP. PHP is used usually in combination with other languages. According to TIOBE Index PHP use is on the rise, with its ranking increasing from 9 to 6 between May 2015 and May 2016. Oracle database is the most commonly used commercial database. PHP and Oracle database make a combination suitable for developing web applications. The PHP OCI8 extension may be used to connect to Oracle database 12, 11g,10g, 9i and 8i using the Oracle Call Interface (OCI). The PHP extension for the Oracle database is included in the PHP 5 installation and may also be downloaded separately. The Oracle PHP extension provides features such as connection caching (pooling), privileged connections using external credentials, statement caching and row pre-fetching. In this tutorial we shall discuss using PHP with Oracle Database. Setting the Environment The following software is required for this tutorial. -PHP -PHP Extension for Oracle Database -Oracle Database 11g -Web Browser (Google Chrome used) Operating system Windows 7 is used in this tutorial. Install Oracle Database 11g. Installing PHP A web server is required for running PHP scripts and PHP 5.4, and later versions include a web server packaged in the PHP installation. Download PHP 5.5 (5.5.33) VC11 x64 Thread-Safe version of the PHP zip file php-5.5.33-Win32-VC11-x64.zip from http://windows.php.net/download/ . The version could vary slightly such as PHP 5.5.35. The VC11 builds require the Visual C++ Redistributable for Visual Studio 2012 x86 or x64 installed as a prerequisite. Extract the php-5.5.33-Win32-VC11-x64.zip file to a directory. A php-5.5.33-Win32-VC11-x64 directory gets created. The directory would be different if a different version is used. Create a document root directory ( c:\php used in this article) and copy the files and directories from the php-5.5.33-Win32-VC11-x64 directory to the c:\php directory. Rename the PHP configuration file php.ini-development or php.ini-production in the root directory of the PHP installation, c:\php , to php.ini . Start the packaged web server at port 8000 with the following command from the document root directory c:\php directory. php -S localhost:8000 The output from the command indicates that the Development Server has been started and listening on http://localhost:8000 as shown in following illustration. In a new command window output the PHP version with the php –version command. Any PHP script copied to the document root directory ( c:\php ) may be run on the integrated web server. PHP scripts may be copied to a subdirectory of the document root directory and run by including the directory path starting from the document root in the URL. Copy the following script hello.php to the C:\php\scripts directory in the document root. PHP Test Hello PHP '; ?> Run the script with the URL http://localhost:8000/scripts/hello.php . The output is shown in the web browser. Installing PHP Extension for Oracle Download the PHP Extension for Oracle zip file php_oci8-2.0.6-5.5-ts-vc11-x64.zip from https://pecl.php.net/package/oci8/2.0.6/windows . Extract the php_oci8-2.0.6-5.5-ts-vc11-x64.zip file to a directory. Copy the php_oci8_11g.dll to the c:/php/ext directory. In the php.ini file uncomment the following line by removing the following line. extension=php_oci8_11g.dll; Also set the extension directory to ./ext in the php.ini file. extension_dir = "./ext" Which Connection Method to Use? The OCI8 PHP class library provides the oci_connect() , oci_pconnect() and oci_new_connect() methods to connect to the Oracle database. If a non-persistent connection is required that is cached, use the oci_connect() method. When the oci_connect() method is invoked with the same parameters more than once in the same script, the same connection resource is returned. Caching of connections improves connection efficiency. The cache used by oci_connect() is deallocated when a script has run or when the connection resource is closed. If transactional isolation between two set of queries is required use the oci_new_connect() method, which returns a new connection. A connection returned by oci_new_connect() is not cached. If a persistent connection that is cached across requests is required, use the oci_pconnect() method. A persistent connection does not get closed when the script has run or with the oci_close(resource connection) method. A persistent connection reduces the overhead of opening and closing connections, thus improving performance. However, if an application connects to the Oracle database using different set of credentials for different web users, the number of connections in the persistent cache increases to the point that the performance of the Oracle server decreases due to the many idle connections. Tune the persistent connections by setting limit on the maximum number of persistent connections in the cache with the oci8.max_persistent configuration directive, and reducing the number of idle persistent connections with the oci8.persistent_timeout configuration directive. Creating a Database Table In this section we shall create an Oracle database table. Create a PHP script, createTable.php , in the C:\php\scripts directory, the document root directory. Define variables $username , $password , and $db for Oracle database username, password and database. Specify the $db variable value as the database SID value in the /NETWORK/ADMIN/tnsnames.ora file. $username='OE'; $password='OE'; $db='(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE) ) )'; If the HOST , PORT or SERVICE_NAME are different than those specified in the PHP script, modify the settings in the script. Connect with the database using the oci_connect() method. $connection = oci_connect($username, $password, $db); If an error occurs in obtaining a connection with the Oracle database, retrieve the error using the oci_error() function and output an error message. if (!$connection) { $e = oci_error(); echo htmlentities($e['message']); } Prepare an Oracle statement to create a database table using the oci_prepare() function. $stmt = oci_parse($connection, "CREATE TABLE OE.WLSLOG(time_stamp VARCHAR2(255) PRIMARY KEY,category VARCHAR2(255),type VARCHAR2(255),servername VARCHAR2(255), code VARCHAR2(255),msg VARCHAR2(255))"); If an error occurs in creating the statement retrieve the error using the oci_error() function and output an error message. if (!$stmt) { $e = oci_error($connection); echo htmlentities($e['message']); } Execute the SQL statement using the oci_execute() method. $r = oci_execute($stmt); If an error occurs in generating the table retrieve the error using the oci_error() function and output an error message. if (!$r) { $e = oci_error($stmt); echo htmlentities($e['message']); }else{ echo " Created table\n\n"; } Prepare an SQL INSERT statement to add a row of data to the Catalog table using the oci_parse() method. $sql = "INSERT INTO OE.WLSLOG(time_stamp,category,type,servername,code,msg) VALUES('Apr-8-2014-7:06:16-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to STANDBY')"; $stmt = oci_parse($connection, $sql); If an error occurs in adding table data retrieve the error using the oci_error() function and output an error message. if (!$stmt) { $e = oci_error($connection); echo htmlentities($e['message']); } Run the SQL statement using the oci_execute() function. $r = oci_execute($stmt); Similarly add another row of data. The PHP script, createTable.php , to create an Oracle database table is listed below. Start the web server, if not already started. Run the PHP script in a browser with URL http://localhost:8000/scripts/createTable.php . A database table, Catalog , gets generated and data gets added to the table. Querying Oracle Database Table Start SQL*Plus CLI with the following command. sqlplus The SQL> command prompt gets displayed. Describe the OE.WLSLOG table generated. DESC OE.WLSLOG; The table description gets listed. Query the OE.WLSLOG table with the following command. SELECT * FROM OE.WLSLOG; The table data gets listed. Retrieving Data from the Database Next, we shall use the PHP Oracle extension to connect with the Oracle database and retrieve data from the database table Catalog that we created in the previous section. Create a PHP script, getTableData.php , in the scripts directory. Define variables $username , $password , and $db for Oracle database username, password and database. Specify the $db variable value as the database SID value in the /NETWORK/ADMIN/tnsnames.ora file. $username='OE'; $password='OE'; $db='(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE) ) )'; Obtain a connection with the database using oci_connect function. $connection = oci_connect($username, $password, $db); Prepare an Oracle statement to select a rows of data. The oci_parse(connection, query) function is used to compile a statement. The prepared SQL statement uses a bind variable, : servername , for the servername column. $stmt = oci_parse($connection, "SELECT * from OE.WLSLOG WHERE servername=:servername"); Bind a value to the :servername bind variable using the oci_bind_by_name() function. oci_bind_by_name($stmt, ":servername", $servername); Run the SQL query with the oci_execute(statement) function. $r = oci_execute($stmt); Fetch all the rows in the result set using the oci_fetch_all(statement, result) function. $nrows = oci_fetch_all($stmt, $results); The oci_fetch_all function returns the number of rows in the result set. If number of rows is more than 0, create an HTML table to output the rows in the result set. By default, the oci_fetch_all() function fetches data by column. Iterate over the rows, and add the column data to the HTML table. if($nrows>0){ echo " Timestamp Category Type Servername Code Msg "; for ($i = 0; $i \n"; foreach ($results as $data) { echo " $data[$i] \n"; } echo " \n"; } echo " "; } We used the oci_fetch_all() method to retrieve data from the result set. But one of the other fetch methods; oci_fetch() , oci_fetch_row() , oci_fetch_array() , or oci_fetch_object() , or oci_fetch_assoc() may also be used. The PHP script, getTableData. php is listed below. 0){ echo " Timestamp Category Type Servername Code Msg "; for ($i = 0; $i \n"; foreach ($results as $data) { echo " $data[$i] \n"; } echo " \n"; } echo " "; } ?> Next, run the getTableData.php script in the web browser with the URL http://localhost/getTableData.php . Oracle database data gets retrieved and displayed in an HTML table. PHP provides the OCI8 extension for the Oracle database. The OCI8 extension may be used to connect to Oracle database with a non-persistent connection or a persistent connection. A connection to Oracle database may be a cached connection or a non-cached connection. Using the OCI8 PHP functions SQL statements may be run to create a database table, add data to the database table and retrieve data from the database table.
↧