Connecting to Oracle 18c Database with OCI8 - php

I have set up an Oracle 18c database and am trying to connect to it from a php file but when I run a simple connection test, I receive a server error where it cant't seem to connect. I ran print_r(getLoaded_extensions()); and from the output array it shows that I am currently not using the oci8 extension as I wanted. My connection test file contains the following
#!/usr/local/bin/php
<?php
putenv("ORACLE_HOME=/usr/lib/oracle/18.3/client64")
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ***.***.*.**)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;
if($c = OCILogon("username", "password", $db))
{
echo "Successfully connected to Oracle.\n";
OCILogoff($c);
}
else
{
$err = OCIError();
echo "Connection failed." . $err[text];
}
I am unsure whether I set my putenv() wrong to the correct location of the oci.dll file or if I need to install the extension in the first place. Thank you

Since you said that you checked your currently used extensions and OCI8 is not present, I would go ahead and install the module and enable it on your server

Most probably, the 18c default installation creates a Container DB (CDB) called ORCL and PDB1 (pluggable DB)..
Run lsnrctl stat to look for the services created.
then, in DB connection, use service_name instead of SID for connection. The value of service_name can be seen in the output of lsnrctl stat
Example.
$db = "(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = patronus.domain.com)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pdb1.domain.com)
)
)" ;

Related

Connecting to Oracle using oci_connect

I am trying to make a successful Oracle connection using PHP.
Here is how my connection string looks:
<?php
$conn = oci_connect("USER", "PASS", "LOSINGMINDHOST");
if (!$conn) {
$e = oci_error();
error_log(trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR));
}
oci_close($conn);
?>
Getting the following error:
Warning: oci_connect(): ORA-12170: TNS:Connect timeout occurred
Which points to the line with oci_connect.
I am on a Windows Server 2019.
The php.ini file has been updated to include the following:
extension=oci8_12c
I have confirmed that the dll file above is indeed in the ext folder listed as:
php_oci8_12c.dll
Not sure why the php.ini file does not include the full name of the dll file.
The server has been installed with Ocale 12g instant client 64bit.
We have confirmed a connection using ODBC Data Source Administrator client using tnsnames.ora file.
We also have a listener.ora file the looks like this:
PROD_MIR =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (COMMUNITY = ttp.world)(PROTOCOL = TTP)(Host = LOSINGMINDHOST)(Port = 1524))
(ADDRESS = (COMMUNITY = ttp.world)(PROTOCOL = TTP)(Host = LOSINGMINDHOST)(Port = 1551))
(ADDRESS = (COMMUNITY = ttp.world)(PROTOCOL = TTP)(Host = LOSINGMINDHOST)(Port = 1538))
)
(CONNECT_DATA =
(SERVICE_NAME = PROD)
)
)
We added the TNS_ADMIN path to the environment variables on the server.
We have already restarted the services and even rebooted the server.
As the docs (https://www.php.net/manual/en/function.oci-connect.php) says:
oci_connect ( string $username , string $password [, string $connection_string [, string $character_set [, int $session_mode ]]] ) : resource
connection_string
Contains the Oracle instance to connect to. It can be an » Easy Connect string, or a Connect Name from the tnsnames.ora file, or the name of a local Oracle instance.
It was your case, use the proper tnsnames.ora Connection name.

How to connect to Oracle Database with php?

I am currently working on a website that connects to an Oracle Database. I have two php files, one for connection with the database and the other is the html structure itself.
Connect.php:
<?php
$servername = "//////";
$username = "/////";
$password = "/////";
$conn = oci_connect($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected!";
?>
I have been having a very hard time connecting to the database. I followed the Oracle tutorial and edited the oci8.connection_class = MYPHPAPP in php.ini, but everytime I run the Connect.php, I get the HTTP Error 500. Did I miss anything? What should I do?
Edit 1: I used display_errors and the error I am getting is Call to undefined function oci_connect()
Edit 2: I tried everything at this point to make the oci_connect work. I downloaded the oracle client and made it an environmental variable but oci_connect is still not working. I would really appreciate if any mac users could help me with this.
Download the appropriate oracle client to your machine, extract it and copy and paste in your system drive.
Add the path of your oracle client to environment variables.
Then you need to enable php_oci8_12c extension using php.ini or GUI if available.
Open php file and write the following code:
function connect(){
$dburl = "(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = your_host)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = your_db_sid)
)
)";
//db charset is optional
$db_charset = 'WE8MSWIN1252'; //your db charset goes here
try {
return oci_connect("username", "password",$dburl,$db_charset);
}
catch (Exception $e) {
die($e);
}
}
This code is work fine on my windows 10 machine, php 7.1.9 and oracle 12c.
Forth link in google after searching for your exact questions brought up the following link: http://me2learn.wordpress.com/2008/10/18/connect-php-with-oracle-database/
<?php
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.34)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;
if($c = OCILogon("system", "your database password", $db))
{
echo "Successfully connected to Oracle.\n";
OCILogoff($c);
}
else
{
$err = OCIError();
echo "Connection failed." . $err[text];
}
?>

PHP and Oracle - Connection as SYS should be as SYSDBA or SYSOPER

I am getting the above error when trying to create my Master repository using an Oracle 11g database.
I am entering SYS as the user, with the correct password and all. However, it is telling me SYS must connect as SYSDBA or SYSOPER.
I went into my user management in the 11G, and made sure that SYS has SYSDBA and SYSOPER checked under the "priveleges" section. I keep getting the same error though.
Any ideas?
<?php
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 1.1.1.1)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;
if($c = OCILogon("sys", "your database password", $db))
{
echo “Successfully connected to Oracle.\n”;
OCILogoff($c);
}
else
{
$err = OCIError();
echo “Connection failed.” . $err[text];
}
?>
Thanks.

PHP - oci_connect not found

I researched and found out oci_connect() is the way to go. I found out that i could either use the Connect Name from the tnsnames.ora file or use an easy connect syntax. Since my database isn't locally stored and I had no idea where the said, tnsnames.ora file was located in apex.oracle.com, I went with easy connect strings.Here's what I've done so far.
$username = "myemail";
$host = "apex.oracle.com";
$dbname = "name";
$password = "password";
// url = username#host/db_name
$dburl = $username . "#".$host."/".$dbname;
$conn = oci_connect ($username, $password, $dburl);
if(!$conn) echo "Connection failed";
I get a
Call to undefined function oci_connect()
So what would be the way to go?
UPDATE 1:
Here's the list of things I did:
Installed Oracle DB
Unzipped Oracle Instance client
Set the environment variables
Uncommented the extension=php_oci8_12c.dll in php.ini
Copied all the *.dll files from the instance client folder to xampp/php and xampp/apache/bin
also made sure the php/ext folder had the required dlls.
That was last night. I have restarted my PC multiple times, APACHE with it but I'm still getting this error:
Call to undefined function oci_connect()
At this point I'm frustrated and don't know where to go from here. PHP just doesn't seem to link up to oci8. I can view the databases I made from Database Configuration Assistant in cmd from 'sqlplus' command and a few select statements. So everything seems to be setup right, its just the php that's having problems trying to use oci_connect().
My database.php, now is setup as:
public function __construct()
{
error_reporting(E_ALL);
if (function_exists("oci_connect")) {
echo "oci_connect found\n";
} else {
echo "oci_connect not found\n";
exit;
}
$host = 'localhost';
$port = '1521';
// Oracle service name (instance)
$db_name = 'haatbazaar';
$db_username = "SYSTEM";
$db_password = "root";
$tns = "(DESCRIPTION =
(CONNECT_TIMEOUT=3)(RETRY_COUNT=0)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = $host)(PORT = $port))
)
(CONNECT_DATA =
(SERVICE_NAME = $db_name)
)
)";
$tns = "$host:$port/$db_name";
try {
$conn = oci_connect($db_username, $db_password, $tns);
if (!$conn) {
$e = oci_error();
throw new Exception($e['message']);
}
echo "Connection OK\n";
$stid = oci_parse($conn, 'SELECT * FROM ALL_TABLES');
if (!$stid) {
$e = oci_error($conn);
throw new Exception($e['message']);
}
// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
throw new Exception($e['message']);
}
// Fetch the results of the query
while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
$row = array_change_key_case($row, CASE_LOWER);
print_r($row);
break;
}
// Close statement
oci_free_statement($stid);
// Disconnect
oci_close($conn);
}
catch (Exception $e) {
print_r($e);
}
}
And it outputs:
oci_connect not found
OCI8 is listed in my phpInfo().
Okay I found out the culprit behind this whole ordeal. I had set the PATH Environment Variables but apparently forgot to add a new system environment variable named TNS_ADMIN and set the directory to PATH/TO/INSTANCE/CLIENT.
Here's the list of System Environment variable you need to add:
Edit PATH system variable and add the $ORACLE_HOME/bin dir
Edit PATH system variable and add the Instance Client dir
Add new system variable, name it TNS_ADMIN and add the Instance Client dir
I hope this helps those who come looking.
First, this has been asked before, but Oracle doesn't allow remote database connections to their free apex.oracle.com example service. Sorry. You can only interact with it through the web interface.
Second, if you do find a remote Oracle db to connect to, you'll need to install the Oracle Instant Client for your OS, and configure the PHP OCI8 extension.

How do install pdo_sqlsrv driver in elastic beanstalk to connect SQL server db instance

I am trying to connect my SQL server db instance to my PHP application in elastic beanstalk.It is giving me an error "driver 1 not found". I am using following code to connect my db instance inside same environment.
<?php
$dbhost = $_SERVER;
$dbport = $_SERVER;
$dbname = $_SERVER;
$charset = 'utf8' ;
$dsn = "sqlsrvl:host={$dbhost};port={$dbport};dbname={$dbname};charset={$charset}";
$username = $_SERVER;
$password = $_SERVER;
$pdo = new PDO($dsn, $username, $password);
?>
AWS developer guide mentions to install a driver for PDO_SQLSRV. I tried installing driver by using .ebextensions folder in my application root directory with a .config file to install the package.
packages:
yum:
php56-mssql: []
but this result in degrading my environment health to server.
I used this links as my reference
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP.rds.html
http://techqa.info/programming/question/35984661/how-to-connect-aws-elb-to-rds-running-ms-sql
Is there any other way to install pdo_sqlsrv driver and extension in elastic beanstalk php application ?
I am new to aws .Please help me to resolve this problem it would bee appreciated.
Addition info:
My platform: 64bit Amazon Linux 2016.09 v2.3.3 running PHP 7.0
After trying multiple answers, I figured out the solution for this issue.
The problem was PHP 7.0 version, for which I had to use package
commands:
00install_mssql:
command: yum install -y php70-mssql
Adding the above code in my application root directory .ebextensions/any.config file
In my case, the amazon document did not helped me because it mentions PDO_SQLSRV driver to be install but they do not support that driver instead I had to use PDO_DBLIB driver below is an example of it.
$dbhost = 'XXXXXXXXXXX.XXXXXXXXXXXXXXX.amazonaws.com:1433';
$dbname = 'dbname';
$charset = 'utf8' ;
$username = 'usernam';
$password = 'password';
$pdo = new PDO("dblib:host=$dbhost;Database=$dbname", $username, $password);
$pdo->setAttribute( PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL);
$tsql ="Select InvID, Number, ProfileNum, PName, InvDate, Des
FROM dbname.dbo
WHERE ProfileNum ='" . $client_no . "'
AND InvDate BETWEEN '". $inv_from_date."' AND '".$inv_to_date."'
ORDER BY InvID";
ini_set('max_execution_time', 300);
/* Execute the query.*/
$stmt = $pdo->query( $tsql );
if ( $stmt->execute() )
{ echo "Statement executed.<br>\\n"; }
else
{ echo "Error in statement execution.\\n";
die( print_r(DBLib_error(), true)); }
$title = $stmt->fetchColumn(3);
print "Client Number :" . $client_no . " <br />";
print" Client Name &nbsp&nbsp :" . $title. ""; // Displaying Client Information

Categories