PHP connection string to remote database ORA-12154 - php

I have 2 computers:
PC 1 - Here is where I installed the XAMPP.
PC 2 - Here is where my database which is Oracle 9i is installed.
I am using PHP 7 and already added PDO_OCI extension.
Here is my connection string:
define("DB_HOST", "192.168.10.30:1521");
define("DB_NAME", "BACKEND");
define("DB_USER", "sa");
define("DB_PASS", "sa_backend");
new PDO('oci:dbname='. DB_NAME . ';host='. DB_HOST .';', DB_USER, DB_PASS);
When I used this code I am getting this error:
Warning: Uncaught PDOException: SQLSTATE[42S02]: pdo_oci_handle_factory: ORA-12154: TNS:could not resolve the connect identifier specified (ext\pdo_oci\oci_driver.c:709)
UPDATE 1
$server = "192.168.10.30";
$db_username = "sa";
$db_password = "sa_backend";
$service_name = "backend";
$sid = "backend";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SERVER = SHARED) (SID = $sid)))";
$this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
I used this code I found on other stackoverflow link. Now I am getting this error
SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12520: TNS:listener could not find available handler for requested type of server (ext\pdo_oci\oci_driver.c:728)
I tried to updated the process to 200 even to 400 but the error is still the same.

Related

Using LOB data in PHP with PDO instead of OCI_Connect

I was using OCI_Connect to connect to my Oracle database.
Because of some internal plicies, i need to change it to PDO.
With OCI_Connect, i can read LOB data from database with "->load()" function in the result, something like this:
$this->Conn = oci_connect($this->User, $this->Pass, $this->Name, 'AL32UTF8');
$sql = "select field from table";
$s = oci_parse($this->Conn, $sql);
$res = oci_fetch_array($s, OCI_ASSOC + OCI_RETURN_NULLS)
echo $res[0]['FIELD']->load();
and it worked very well.
Now i need to do the same stuff with PDO, and because all my queries may change the number and name of the fields, i cannot bind the variables before executing it.
What i'm using now to connect:
$dbTns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";
$paramArray = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$this->PDO = new PDO("oci:dbname=" . $dbTns . ";charset=utf8", $db_username, $db_password, $paramArray );
With PDO, everything works fine, but i can't use the "->load()" function in the LOB field, as it does not exists here.
Is there an equivalent way to get the data after the query run?
Any suggestions are welcome.
(yes, i did search for a solution before posting that question here)

Connect to MariaDB by using PDO with accept server cert

I can connect the database with Maria client with --ssl options on application server:
mariadb -h [Address] -P [Port] -u [DB admin user] -p --ssl
But tried to connect the same database on same server with PHP PDO, we got the error message with:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client[client_ed25519]
I have added below options to PDO connection for simulate the --ssl options
PDO::MYSQL_ATTR_SSL_CA => true,
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
Here is my testing code piece:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$options = [
PDO::MYSQL_ATTR_SSL_CA => true,
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
];
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password, $options);
// 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();
}
?>
Database version: MariaDB 10.5

php ssl error when trying to connect to xmpp ejabberd server

i tried to connect to my xmpp server using php with this library: https://github.com/fabiang/xmpp but get this following error:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /var/www/html/cobaxmpp/vendor/fabiang/xmpp/src/Stream/SocketClient.php on line 210
here is my code
<?php
$logger = new Logger('xmpp');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$hostname = 'im2.hprasetyou.com';
$port = 5222;
$connectionType = 'tcp';
$address = "$connectionType://$hostname:$port";
$username = 'tom';
$password = 'password';
$options = new Options($address);
$options->setLogger($logger)
->setUsername($username)
->setPassword($password);
$client = new Client($options);
$client->connect();
$client->send(new Roster);
$client->send(new Presence);
$client->send(new Message);
$client->disconnect();
what am i missing?

PHP, PDO => MSSQL with 2 instances

i wrote a simple script to query some data from a MSSQL-Express 2012 Server.
All workes fine with the first instance:
$host = "192.168.13.3\test1";
$user = "sa";
$passwd = "test1";
self::$instance = new PDO ("dblib:host=$host","$user","$passwd");
but if I try to connect to my second Instance like:
$host = "192.168.13.3\test2";
$user = "sa";
$passwd = "test2";
self::$instance = new PDO ("dblib:host=$host","$user","$passwd");
I get this error:
[message:protected] => SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
[string:Exception:private] =>
[code:protected] => 20009
With MSSQL-Server-Management-Tool I can access both instances without problems.
Has somebody a hint where i can find my failure?
Thanks in advance

Php pdo unixOBDC to sqlserver 2008: String data, length mismatch when execute prepared stmt

I have the following setup: Apache / php 5.3 / pdo with odbc with installed Microsoft SQL Server ODBC Driver 1.0 for Linux on server.
My script rises an error with the following stacktrace when trying to execute a statement:
(UTC) 2013-12-16 12:07:40: Thrown exception log ->
'Error message: SQLSTATE[22026]: String data, length mismatch: 0 [Microsoft][SQL Server Native Client 11.0]String data, length mismatch (SQLExecute[0] at /tmp/pear/temp/PDO_ODBC/odbc_stmt.c:133)
Arisen in Core_Db->select(array (
0 =>
'SELECT *
FROM TMS.dbo.TEST_user
WHERE email = ? AND status_id = ?',
1 =>
array (
0 => 'support#mail.com',
1 => 2
),
))
For testing I use php 5.3 on windows with pdo sqlsrv and nothing went wrong there.
Connection code is
// for unixODBC (production)
if (DB_DRIVER == 'odbc') {
$this->_link = new PDO(
"odbc:DRIVER={SQL Server Native Client 11.0};SERVER=" . DB_HOST . ";"
. "PORT=" . DB_PORT . ";DATABASE=" . DB_NAME . ";PROTOCOL=TCPIP;UID=" . DB_LOGIN . ";"
. "PWD=" . DB_PASSWD . ";"
);
// for sqlsrv (development)
} else {
$this->_link = new PDO(
"sqlsrv:Server=" . DB_HOST . "," . DB_PORT . ";Database=" . DB_NAME,
DB_LOGIN,
DB_PASSWD
);
}
$this->_link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
There is some recommendation regarding this issue: ODBC and SQL Server 2008: Can't use prepared statements?. But I can't check it. When I try to add an attribute in production, the script rises the following error:
(UTC) 2013-12-16 13:19:44: SQLSTATE[IM001]: Driver does not support this function: driver does not support setting attributes
Does anyone know how to resolve this problem?
UPDATED 2013-12-18
// unixODBC connection
$this->_link = new PDO(
"odbc:DRIVER={SQL Server Native Client 11.0};SERVER=xxx.xxx.xxx.xxx;"
. "PORT=1433;DATABASE=TMS;PROTOCOL=TCPIP;",
DB_LOGIN,
DB_PASSWD,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // this attr is recognized by ODBC
PDO::ATTR_EMULATE_PREPARES => false // but if this is added ODBC throws PDOException with message "driver does not support setting attributes"
)
);
String's transfer from ODBC to Client leads to string's corruption while prepared statement is used. So I only guess the matter is in PDO::ATTR_EMULATE_PREPARES.
The error
(UTC) 2013-12-16 13:19:44: SQLSTATE[IM001]: Driver does not support
this function: driver does not support setting attributes
is telling you that this line:
$this->_link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
is invalid. Instead, do this:
$driver_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => FALSE);
if (DB_DRIVER == 'odbc'){ // for unixODBC (production)
$dsn = 'odbc:DRIVER={SQL Server Native Client 11.0};SERVER='. DB_HOST .';PORT='. DB_PORT .';DATABASE='. DB_NAME .';PROTOCOL=TCPIP;';
$this->_link = new PDO($dsn, DB_LOGIN, DB_PASSWD, $driver_options);
}else{ // for sqlsrv (development)
$this->_link = new PDO(
"sqlsrv:Server=" . DB_HOST . "," . DB_PORT . ";Database=" . DB_NAME,
DB_LOGIN,
DB_PASSWD
);
}

Categories