Connect to MariaDB by using PDO with accept server cert - php

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

Related

Connecting to PGSQL over SSL via Red Bean PHP

$dbh = new PDO('pgsql:localhost=host;port=26257;dbname=bank;sslmode=require;sslcert=[path]/client.maxroach.crt;sslkey=[path]/client.maxroach.key;sslrootcert=[path]/ca.crt;',
'maxroach', null, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true,
));
This a pdo method, i need configure red bean connection for ssl pgsql connection
R::setup( "pgsql:host=$ip;port=$port;dbname=$dbname",$user, $password, $frozen ); ?
You definitely need to write the full path to certificates and keys,
otherwise nothing will work.
$crt = $_SERVER["DOCUMENT_ROOT"]."/client.crt";
$key = $_SERVER["DOCUMENT_ROOT"]."/client.key";
$ca = $_SERVER["DOCUMENT_ROOT"]."/ca.crt";
R::setup( "pgsql:host=$ip;port=$port;dbname=$dbname;sslmode=verify-ca;sslcert=$crt;sslkey=$key;sslrootcert=$ca;",$user, $password, $frozen );
sslmode=verify-ca; better use sslmode=verify-full

PHP connection string to remote database ORA-12154

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.

PHP PDO_SQLSRV: encoding error with column containing accented chars

I'm getting back "�" instead of accented chars in my PSO_SQLSRV querys. This is how i connect to the db:
$dsn = 'sqlsrv:Server=' . $this->host . ';Database=' . $this->dbname;
`$options` = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch (PDOException $e) {
new DatabaseErrorHandler($e);
}
I tried adding PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8 to my $options array, but nothing changes. Db collation is Latin1_General_CI_AS.
I'm using Microsoft driver php_pdo_sqlsrv_56_nts.dll with PHP 5.6.14

Jommla connect to another database using Multisites extension

I'm using Joomla 2.5 and Multisites extensions. When I'm on page X Joomla is using X-database, on page Y using Y-database - it's ok. Unfortunately I have to switch to another database. How I said $db = JFactory::getDbo(); connect to current site database, because Multisites extension works that every page has his own configuration file with database parameters. Any ideas?
Here is solution:
$option['driver'] = 'mysqli';
$option['host'] = 'localhost';
$option['user'] = 'root';
$option['password'] = '';
$option['database'] = 'joomla';
$option['prefix'] = 'a45gy_';
JFactory::destroy();
$db = JDatabase::getInstance($option);
if (JError::isError($db)) {
jexit('Database Error: ' . $db->toString());
}
if ($db->getErrorNum() > 0) {
JError::raiseError(500, 'JDatabase::getInstance: Could not connect to database <br />');
}
parent::setDbo($db);
try it.
$dbX = JDatabase::getInstance(array ('host' => $host, 'user' => $user, 'password' => $pass, 'database' => $database, 'prefix'=>null));
$dbY = JDatabase::getInstance(array ('host' => $host, 'user' => $user, 'password' => $pass, 'database' => $database, 'prefix'=>null));

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