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.
Related
I want to install the db2 extension for PHP on Windows but it just isn't working. I've tried a lot of different solutions but I still get these:
php.exe
phpinfo()
When I try connection to db2 database
I'm using Apache on XAMPP on port 80. I have installed this: https://github.com/ibmdb/php_ibm_db2/tree/master/PHP%207.4.x/x64/TS
and placed it in my C:\xampp\php\ext folder
and set "extension=php_ibm_db2.dll" in my php.ini file. (which is in C:\xampp\php)
Only "db2" appearing in phpinfo()
Variable where db2 is appearing
PATH variable
So, I did not manage to connect to my db2 DB with the db2 extension, but here's an alternative I've found -->
$database = "xxx";
$hostname = "xxx";
$user = "xxx";
$password = "xxx";
$port = 50000;
# Build the connection string
$driver = "DRIVER={IBM DB2 ODBC DRIVER};";
$dsn = "DATABASE=$database; " .
"HOSTNAME=$hostname;" .
"PORT=$port; " .
"PROTOCOL=TCPIP; " .
"UID=$user;" .
"PWD=$password;";
$conn_string = $driver . $dsn;
# Connect
$conn = odbc_connect( $conn_string, "", "" );
if( $conn )
{
echo "Connection succeeded.";
odbc_close( $conn );
}
else
{
echo "Connection failed.";
}
output -->
Connection succeeded.
source -->
https://www.ibm.com/docs/en/db2woc?topic=programmatically-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)
)
)" ;
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.
I am new with using OBDC in php. I have an error upon connection:
[function.odbc-connect] SQL error [Microsoft][ODBC Microsoft Access Driver] Invalid string or buffer length
PHP ODBC
$string_serve = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = host.com)(PORT = 1404)))
(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = SERVE)))";
$string_serve ="DRIVER={SQL Server};
SERVER='server.com';
DATABASE= db;"
$conn = odbc_connect($string_serve, 'user', 'pass');
I have tried using both variables
I recommend to keep the connection details out of the source code.
Create a .odbc.ini file somewhere not publicly accessible. Define a data source like this:
[my_database]
Driver = /usr/lib/libmyodbc3.so
SERVER = server.com
DATABASE = db
Let the odbc driver find it:
putenv('ODBCINI=/your-application-path/etc/.odbc.ini');
Using exported variables at Apache-startup etc. might not work as avariables are sometimes not passed to the application.
Use a simple odbc_connect() in your PHP code:
$conn = odbc_connect('my_database', 'username', 'password')
I'm using the following code:
try {
$hostname = "*****";
$port = 1443;
$dbname = "*******";
$username = "********";
$pw = "************";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
Error:
Failed to get DB handle: could not find driver
The DLL Files are already in the php/ext and modified in the php.ini
What else can I do?
From the port you're using, it's clear that you're trying to connect to an MSSQL database.
PHP documentation states that the PDO support for connecting to SQLSRV is not recommended for windows machines running php 5.3 or older.
http://php.net/manual/en/ref.pdo-dblib.php
They do suggest using sqlsrv drivers provided by microsoft.
Link to sqlsrv drivers are in the php link above.
Have you tried mssql_connect?
Here's the syntax: mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )
The implementation would be:
$link = mssql_connect($hostname, $username, $pw);
mssql_select_db($dbname, $link);
Note that $hostname should contain MSSQL Instance Name. e.g. 'KALLESPC\SQLEXPRESS'