I am trying to connect to an oracle database using php. When I try to connect by connection string/descriptor, it connects just fine, but when I try to connect using a tns name, the whole thing stops and chrome shows a blank error page ERR_CONNECTION_RESER. I see the failed response and it is 0 bytes, no header, no body... nothing.
here is the code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$connStr = "Some connection <string> copied from tnsnames.ora";
//$connStr = "Some connection <name> copied from tnsnames.ora";
try
{
$conn = oci_connect('dev', '12345678', $connStr, 'utf8');
if (!$conn)
{
$err = oci_error();
var_dump($err);
}
oci_close($conn);
echo "Success";
}
catch(Exception $e)
{
$err = oci_error();
var_dump($err);
var_dump($e);
}
I checked and my php can see
ORACLE_HOME
TNS_ADMIN
and they are pointing to the right direction.
I am using
Windows 7 amd64
PHP 5.6.13 TS VC11
Apache 2.4.16 VC14
Instant Client 12.1.0.2
After hours spent and sleeps not slept on this issue, I found the solution.
I have an sqlnet.ora file that specifies timeouts and other network properties. Our databases recently joined the firm's domain and the problem was with this line:
Previously
NAMES.DIRECTORY_PATH= (HOSTNAME, TNSNAMES, ONAMES, EZCONNECT, LDAP)
#########
Solution
NAMES.DIRECTORY_PATH= (HOSTNAME, TNSNAMES, ONAMES, EZCONNECT, LDAP)
NAMES.DEFAULT_DOMAIN = FIRM.DOMAIN
Related
I need to connect from PHP to an SQL Server 2008 instance. The Webserver is Apache 2.4 on a Centos 8 with PHP 7.4, php-sqlsrv extension, and I am using PDO to handle the connection.
First I tested the connection from cli, and I got the following error:
Connection failed: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol]
A little bit of googling sent me to a direction toward openssl.cnf, where I set the minimum version to TLS 1.0 (I know it's not secure, but it seemed to be working), and then connection worked, but only in cli, when I try it from the site, it still gives the same error.
The code I use for the connection is this (I changed the connection data):
try {
$conn = new PDO("sqlsrv:Server=database.local,1433;Database=database", "user", "password");
}
catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Can you help me figure out what I missed?
Thank you
update-crypto-policies --set LEGACY
I have some problem. I have been looking it for almost a week. I have done include ibm_db2.dll, change php version but not working. When I run the db2 query, it shows Fatal error: Uncaught Error: Call to undefined function db2_connect() in C:\laragon\www\hpc_dev\db2_conn.php:15 Stack trace: #0 C:\laragon\www\hpc_dev\index.php(3): include() #1 {main} thrown in C:\laragon\www\hpc_dev\db2_conn.php on line 15
Currently I am using php version 7.4.12 64bit (ts) using laragon in Windows 10. I also have tested using Xampp and the same problem occur. Below are my codes to connect to ibm db2:
db2_conn.php:
<?php
ini_set("display_errors", 1);
$database = 'DB2';
$user = 'xxx';
$password = 'xxx';
$hostname = 'xx.xx.xx';
$port = 60000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
"HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
echo "Connection failed.";
}
?>
My code to show data from db2:
<?php
include("db2_conn.php");
$sql = "SELECT * FROM EMPLOYER WHERE EMPR_LOGIN_NAME = 'xxxxx'";
if ($conn)
{
$stmt = db2_exec($conn, $sql);
$row = db2_fetch_assoc($stmt);
echo $row['SECTOR_DESC'];
db2_close($conn);
}
?>
I don't know either the problem comes from db2 extension or others. Please help me.
For information, I have download the ibm db2 extension here: https://github.com/ibmdb/php_ibm_db2
Thanks
Your question omits details of the command-lines and actions you performed.
Most likely you missed a step in the configuration.
Remember to open the laragon terminal and verify that the Db2 driver is loaded:
php -m | grep ibm
should return ibm_db2 if you have correctly configured the correct version of the extension.
If you do not see that the ibm_db2 extension is loaded then any db2_connect will fail until you resolve the missing module.
For information the current laragon_full (November 2020) delivers PHP 7.2 TS, and that works correctly with the php_ibm_db2.dll TS for v7.2.x "out of the box" (which you must first download and configure).
Additionally, if I upgrade the PHP version for laragon to PHP 7.4.12 TS, and subsequently download and configure the matching php_ibm_db2.dll TS for php v7.4.x, then it all "just works" without any issues on Win 10 x64 2020 to connect to Db2-LUW and run queries etc.
It helps to verify that your Db2 client is correctly configured. You can do that independently of PHP. If you are using the clidriver Db2-client (the smallest footprint driver) you can configure the db2dsdriver.cfg XML file and then run db2cli validate -connect -dsn -user ... -passwd ... to verify that the connection works. If you are using either the Db2-runtime-client or the Db2 full client, or a local Db2-server, you can run (in the laragon terminal):
(after configuring your Db2 node directory and Db2 db directory via appropriate configuration commands )::
set DB2CLP=**$$**
db2 connect to $yourdsn user ... using ...
I'm attempting to get PHP 7.1 to connect to an instance of SQL Server on the same network, I get the following error:
Connection failed: could not find driver
I've downloaded and added both php_pdo_sqlsrv.dll and php_sqlsrv.dll (both nts versions). I have tried both the most recent release of, AND, the pre-release versions that supposedly fix an error with the .dll files not working with PHP 7.1.
Both files have been added in our php.ini file as well.
PHP file that should connect to the DB..
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$serverName = "######";
// $myUser = "#####";
// $myPass = "######^";
// $dbName = "######";
//connection to the database
try {
$conn = new PDO("sqlsrv:Server=($serverName,53000);port=1433;Database=$dbName", NULL, NULL);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
The PHP.ini section currently looks like this:
extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
;extension=php_sqlsrv_71_nts.dll
;extension=php_pdo_sqlsrv_71_nts.dll
extension=php_pdo_sqlsrv.dll
;extension=php_pdo_sqlsrv_54_nts.dll
;extension=php_sqlsrv_53_nts.dll
extension=php_sqlsrv.dll
php -info only shows odbc under "PDO drivers" - which I believe indicates that it isn't activating.
Have restarted the server multiple times.
Any help is much appreciated, thanks!
I am new to PHP and MySQL and have recently installed both PHP v5.3.10 and MySQL v 5.5.21 on a Windows Server 2003 server already running IIS v6.
PHP runs and I have created a database on MySQL from the MySQL 5.5 Command Line Client.
However, when I try to access the database from PHP with the following commands:
echo "Open database";
$link = mysql_connect($host, $username, $password);
echo " link: $link";
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $link)or die("cannot select DB");
echo " database open";
I get the following returned to the web page:
Open database
No error messages are produced and nothing after the mysql_connect command is returned from
the PHP to the screen.
Also tried line below which also did not return any error messages:
mysql_connect($host, $username, $password) or die('Cannot connect:' . mysql_error());
Anybody have any ideas why I can't make a connection and can't get an error message back from mysql_connect command?
I have checked MySQL and I have tried defining the host as %, localhost, the local host IP and the IP:port number (from the port number 3306 listed in the my.ini) to no effect.
I only have one username of 'root' created in mySQL with a single password (which I used when I opened MySQL to create the database)
The 'php.ini' I have placed in both the 'C:\Program Files\PHP' and 'C:\WINDOWS'.
This file contains 'extension_dir = "C:\Program Files\PHP\ext"' to specify the extension directory and includes the following at the end of the file:
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll
Also I tried running phpinfo() and it returned the following table for mysqlnd:
mysqlnd enabled
Version mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $
Compression supported
SSL supported
Command buffer size 4096
Read buffer size 32768
Read timeout 31536000
Collecting statistics Yes
Collecting memory statistics No
Tracing n/a
So Is I assume the php should be able to connect.
Try this:
ini_set('display_errors',1);
error_reporting(E_ALL);
To mirror #Jrod:
is error_reporting on?
have you checked the error_log for the instance?
It sounds like something's failing and your error reporting isn't set high enough to show it. You might try addin this to the top of the script:
error_reporting(E_ALL);
I am new to php but I've got it running (php 5.3.3) on my mac (OS 10.6). However when I try to run this script:
<?php // Create connection to Oracle
$conn = oci_connect("user", "pass", "tnsnames.ora");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
I get the error: "Fatal error: Call to undefined function oci_connect()..."
Now, I've checked oracle's website and downloaded their instant client, but I'm stuck as to what to do next. Their instructions are:
On non-Windows platforms rebuild PHP
using the following configuration
option:
--with-oci8=shared,instantclient,/path/to/instant/client/libs
Edit your php.ini file and add:
extension = oci8.so Ensure that your
extension_dir parameter (in php.ini)
points to the location where oci8.so
was installed. Set environment
variables required by Oracle, such as
PATH (Windows) or LD_LIBRARY_PATH (on
Linux) Restart you webserver.
But could someone explain that to me in simpler language? I am really confused. I can't find an oci8.so file in the instant client folder, and I don't know where to put the extention = oci8.so in the file (all I have are php.ini-production and php.ini-development. And I don't know how "rebuild" PHP with configuration options.
Oh and I don't know how to pull in the tnsnames.org file either. Is this the right way? Since I don't even have a connection yet, I don't know if this is failing or not.
You don't want the filename "tnsnames.ora" in your oci_connect call. You want the name of a database connection alias in the tnsnames.ora file. The tnsnames.ora file contains a list of known database connection entires, identified by aliases. For a database alias named "db_alias", you would use this call:
$conn = oci_connect("user", "pass", "db_alias");
A typical tnsnames.ora entry would look something like this:
DB_NAME =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = foo.bar.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE = some_db_service_name))
)
Alternatively, you can use an Easy Connect string:
$conn = oci_connect("user", "pass", "//host:port/db_service_name");
Where host, port and db_service_name are to be replaced by values from your environment. So, for host "foo" with database "bar" on standard port 1521, your connection would look like this:
$conn = oci_connect("user", "pass", "//foo:1521/bar");
Have a look at the manual page for oci_connect for more information.
I do it with a full connect string:
$rnum=rand(0,99999999);
$connect_str = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = $dbserver) (PORT = $dbport) (HASH = '.$rnum.') ) (CONNECT_DATA =(SID = $dbname)) )";
$DB = oci_connect($dbuser, $dbpass, $connect_str);
Works like a charm for me.