{"id":1351,"date":"2018-01-19T19:22:44","date_gmt":"2018-01-19T19:22:44","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/oracle-with-php-a-developers-view\/"},"modified":"2018-01-19T19:24:43","modified_gmt":"2018-01-19T19:24:43","slug":"oracle-with-php-a-developers-view","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/oracle-with-php-a-developers-view\/","title":{"rendered":"Oracle with PHP: a Developers View"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Rajeev Ranjan Rakesh<\/div>\n<div class=\"\">on October 17, 2008<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articleHeader\">\nIntroduction\n    <\/div>\n<div class=\"articlePara\">\nThis article is for the developer who wants to use the PHP scripting language with an Oracle database to develop their application. PHP is one of the most commonly used scripting languages, as it is open source and has procedural as well as object-oriented capabilities. Oracle is a popular RDBMS and that&#8217;s the reason this combination is very useful for web-based application development.\n    <\/div>\n<div class=\"articlePara\">\nThere are different methods of using PHP with Oracle, but this document focuses on using OCI8 and the ADOdb abstraction library to connect to Oracle. ADOdb in turn uses the OCI8 oracle extension to connect to the Oracle database. There are multiple tutorials and articles available which explain different methods of using PHP and Oracle, but this one is written considering a simple and efficient way of implementation.\n    <\/div>\n<div class=\"articlePara\">\nThe first question that comes to mind is why we should use the ADODB library if we are already using the OCI8 extension&#8211;and ADOdb interacts using OCI8 internally. The answer is &#8220;it hides all database dependent complexity and can be more easily implemented and understood by a new developer. Additionally, it adds portability to the application.\n    <\/div>\n<div class=\"articleHeader\">\nOCI8 Extension\n    <\/div>\n<div class=\"articlePara\">\nOCI8 is the most popular PHP extension for Oracle, and is recommended for maximum performance. It effectively shows the potential of Oracle&#8217;s features in the application and provides stability. The PHP OCI8 extension is included in PHP version 3 onwards.\n    <\/div>\n<div class=\"articleHeader\">\nConnection Methods for OCI8 extension\n    <\/div>\n<div class=\"articlePara\">\nOCI8 provides different connection methods to connect to Oracle database, as shown below.\n    <\/div>\n<div class=\"articlePara\">\noci_connect() : It is used in the case of non-persistent connection. If this function is called more than once in the script, then the same connection is used, provided the connection is not yet closed.\n    <\/div>\n<div class=\"articlePara\">\noci_pconnect() : It is used in the case of persistent connection. Persistent connections do not get closed even after the completion of the script. It improves the performance as it reduces the overhead of opening and closing the database connection.\n    <\/div>\n<div class=\"articlePara\">\noci_new_connection() : It always creates a new connection to the database even if there is an existing connection. Generally this is not used except in some cases where application needs two separate connections.\n    <\/div>\n<div class=\"articleHeader\">\nClosing Oracle connection\n    <\/div>\n<div class=\"articlePara\">\nOci_close() : The connection is opened through the calls oci_connect(). oci_new_connection() gets closed automatically but we can explicitly close them using this function.\n    <\/div>\n<div class=\"articlePara\">\nNote: a connection is opened using method oci_pconnect() cannot be closed using oci_close().\n    <\/div>\n<div class=\"articleHeader\">\nSample example to use OCI8\n    <\/div>\n<div class=\"articlePara\">\nNow we will show you how to connect to the Oracle database using OCI8 oracle extension and we will execute a select statement which select some fields and we will display it on the web page.\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n&lt;?\n\n$dbuser = \"dbuser\";\n$dbpassword = \"dbpassword\";\n$db = \"oracle_sid\";\n\n$conn??=??oci_connect($dbuser,$dbpassword,$db);\n\nif (!$conn){\n\techo \"Connection error\";\n\texit;\n}\n$sql = \"SELECT??*??FROM??EMPLOYEE_TABLE\";\n\n$stmt??=??oci_parse($conn,??$sql);\n\nif??(!$stmt)??{\n\techo \"Error in preparing the statement\";\n\texit;\n}\n\noci_execute($stmt,??OCI_DEFAULT);\n\nwhile($result =oci_fetch_array($stmt)) {\n\techo $result[??EMPLOYEE_NAME??] . \"&lt;br&gt;\";\t\n}\n\noci_close($conn); # optional\n\n?&gt;\n\n<\/pre>\n<p><\/code>\n    <\/div>\n<div class=\"articleHeader\">\nCode walkthrough\n    <\/div>\n<div class=\"example\">\n<code><\/p>\n<pre>\n$dbuser = \"dbuser\";\n$dbpassword = \"dbpassword\";\n$db = \"oracle_sid\";\n\n$dbuser : The Oracle user name\n$dbpassword  : The Oracle password\n$db : the database connection string or the alias; \nthe connection string can be stored in the tnsname.ora file. \nFor example, in the tnsname.ora file, we can include \nthe sample connection string as shown below:\n\nCONN_STRING =\n  (DESCRIPTION =\n    (ADDRESS_LIST =\n      (ADDRESS = (PROTOCOL = TCP)(HOST = &lt;host_name&gt;)(PORT = 1521))\n    )\n    (CONNECT_DATA =\n      (SID = CONN_STRING)\n    )\n  )\n\n\n$conn??=??oci_connect($dbuser,$dbpassword,$db);\n<\/pre>\n<p><\/code>\n    <\/div>\n<div class=\"articlePara\">\nPassing the username, password and SID or connection string from the tnsname.ora file to the oci_connect() method. $conn variable store the Returned connection identifier. As per the above explanation we can use the connection string directly or we can use an alias.\n    <\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"rajeevrakesh200810164658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"rajeevrakesh200810164658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article is for the developer who wants to use the PHP scripting language and an Oracle database to develop their application. There are<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1351","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1351"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":3234,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1351\/revisions\/3234"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}