PHP PDO ODBC CONNECT TO PERVASIVE DATABASE - php

All 32Bit. Windows 2018 Server.
PVSQL v10 (I can't update this)
PHP 5.3.28 (I might be able update this to 5.6.x) 7.x Would break too much
I've tried searching everywhere for an example on how to connect PHP to Pervasive DB with PDO. I've managed to connect using the following non PDO connector:
$conn = odbc_connect("demodata","","");
if(!$conn) die("Could not connect");
Looking at the examples from https://docs.actian.com/psql/psqlv13/index.html#page/odbc%2Fodbcadm.htm%23ww1213912
I found that I should be using something like:
try {
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerDSN=demodata;");
if(!$conn) die("Could not connect");
}
catch (PDOException $e) {
echo $e->getMessage();
exit;
}
I also tried:
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=localhost;SeverDSN=demodata;");
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=localhost;SeverDSN=DEMODATA;");
When I do though. I get the error:
SQLSTATE[HY000] SQLDriverConnect: -1206 [Pervasive][ODBC Client
Interface][LNA][Pervasive][ODBC Engine Interface][Data Record
Manager]Non-db file or corrupted db.
But I know its not corrupted.I'm able to connect and do queries the other way. Would just really like to be able to use PDO. When I do a test from the Pervasive ODBC Client DSN Setup. I get Connection Successful.
What am I missing?

Thanks to #Mitheil for pointing me in the right direction. The server name does seem to be a must even on local machine. So the string needed is: "odbc:Driver={Pervasive ODBC Client Interface};ServerName=db;ServerDSN=demodata;"
try {
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=db;ServerDSN=demodata;");
if(!$conn) die("Could not connect");
}
catch (PDOException $e) {
echo $e->getMessage();
exit;
}

Related

Connection error to SQL Server via PHP, SQL STATE IM004, Driver's SQLAllocHandle on SQL_HANDLE_ENV failed

I already have searched and googling about my question, but i still have not find the answer. My problem is when connecting to my SQL Server database via PHP PDO/ODBC Connection, i always getting error : "[Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed, SQL state IM004 in SQLConnect". But my connection to Oracle or MySQL Database are not problem at all, only to SQL Server database.
Here my code to testing connection :
$dbh = null;
try
{
$dbh = new PDO('oci:dbname='.TNS, DB_USERNAME, DB_PASSWORD, null);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
//var_dump($pe->getMessage());
}
if (!is_null($dbh)) {
echo "Oracle Connection success";
}
else {
echo "Oracle cannot connect";
}
echo "<br />";
$dbh = null;
try
{
$dbh = new PDO('mysql:host=localhost;dbname=db_test', 'root', '', null);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
//var_dump($pe->getMessage());
}
if (!is_null($dbh)) {
echo "MySQL Connection success";
}
else {
echo "MySQL cannot connect";
}
echo "<br />";
$dbh = null;
try
{
$dbh = new PDO('sqlsrv:server=XXXXX;Database=xxxxxxx', 'xxxx', 'xxxx', null);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
//var_dump($pe->getMessage());
}
if (!is_null($dbh)) {
echo "MySQL Connection success";
}
else {
echo "SQL Server cannot connect";
}
The results above is shown as :
Oracle Connection success
MySQL Connection success
SQL Server cannot connect
I have tried to reinstall XAMPP and copying the new PHP's SQL Server library. But the error still same. As i mention above, i have been searching the solution, but still no luck. I truly hope, i can get a solution for my problem, or at least a clue for me to started with. Oh yeah, i almost forgot, this error occurs after Windows 10 update, before that, my connection to SQL Server is fine. So, i wonder whether this problem related to Windows update or not. I really have no clue.
Sorry for my English language, and thank you very much for your helps. I really apreciated for any answer
I was having the same issue and these are the steps I took to make sure it was working correctly:
My Config (Win10, XAMPP, PHP 7.2.8)
1) Downloaded the Microsoft PHP Drivers for SQL Server 5.3 and put the following files in the C:\xampp\php\ext directory:
php_sqlsrv_72_ts_x86.dll
php_pdo_sqlsrv_72_ts_x86.dll
2) Added the following lines to my php.ini file in c:\xampp\php
extension=php_pdo.dll
extension=php_sqlsrv_72_ts_x86.dll
extension=php_pdo_sqlsrv_72_ts_x86.dll
Note: I was originally missing the first line (extension=php_pdo.dll) and that gave me a "Driver's SQLAllocHandle on SQL_HANDLE_ENV failed" error. You do not need to put the dll in the ext directory because in PHP 7 it is built-in but you do need to put the line in the ini file.
3) Downloaded and installed Microsoft ODBC Driver for SQL Server 17.
Make sure you stop and restart the Apache server after making the changes.

Connect IBM database using PDO

I tried connect IBM database through PDO using below code. But, it is not working
try {
$db = new PDO("odbc:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=BLUDB;HOSTNAME=hostname;PORT=50000;PROTOCOL=TCPIP;", "username", "password");
echo "<pre>";
print_r($db);
exit;
} catch (PDOException $e) {
echo $e->getMessage();
}
I got below error for the same
SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I have also added below code in php.ini file
extension=php_pdo.dll
extension=php_pdo_ibm.dll
extension=php_ibm_db2.dll
Could anyone suggest me, how I can connect with IBM database?
The DSN prefix for DB2 databases is ibm:, not odbc:. Try changing that.
Here is the example connection string given in the documentation:
$db = new PDO("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=testdb;" .
"HOSTNAME=11.22.33.444;PORT=56789;PROTOCOL=TCPIP;", "testuser", "tespass");
there is different driver name for some computer, this is the list of driver you can try:
DRIVER={iSeries Access ODBC Driver};
DRIVER={IBM i Access ODBC Driver};
and try to use System instead of HOSTNAME
DRIVER={iSeries Access ODBC Driver};DATABASE=BLUDB;System=hostname;PORT=50000;PROTOCOL=TCPIP;
oh, and I'm using DRIVER={IBM i Access ODBC Driver};

Connect PHP on Linux server to MS SQL on remote server running Windows

Im having real trouble getting this to work. I have got the mssql-connect drivers set up and working on the Linux server, it now understands that function and does not return any errors.
I have had our server management team configure the windows server to allow the connection from the linux server. They have tested this and confirm it is all working, however I just cant seem to connect to it with PHP.
I have tried various connection strings, but it won't connect, here is an example
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '214.133.182.71,1443';
// Connect to MSSQL
$link = mssql_connect($server, '****', '******');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I always get:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 214.133.182.71,1443 in /home/v3rec/public_html/test.php on line 10
Something went wrong while connecting to MSSQL
The username and password of that for the MS SQL database. Ive tried it with and without the port name. I am running SQL 2012, im not sure what the instance name would be? Am I missing something? Why is PHP unable to connect?
May I suggest you use PDO and do this:
$dbhost = "myhost";
$dbport = 10060;
$dbname = "tempdb";
$dbuser = "dbuser";
$dbpass = "password";
try {
$dbh = new PDO ("dlib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}

Connecting to MSSQL server via php-pdo?

I'm unable to connect to MSSQL database on smarterasp.net domain. I'm using:
PHP Tools for Visual Studio
Here is the code:
<?php
try {
$conn = new PDO("mssql:host=host_name_string;dbname=database_name_string", "username_string", "password_string");
// 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();
}
?>
and the error caught by the PDOexception class:
Connection failed: could not find driver
I've also tested my code online and again getting the same error(online tester url: http://sandbox.onlinephpfunctions.com).
I've searched for the solution and the only thing that could resolve my problem is by uncommenting:
extension=php_pdo_mysql.dll
but the line is already uncommented by default.
EDIT:
The following does not ressolve my problem (instead of mssql:host)
sqlsrv:host
dblib:host
Your problem is that you have not installed either the sql server client or the Microsoft pdo drivers on your machine. Please do that and make sure you can connect via a udp file.
Search SQL server client install and Microsoft pdo drivers

Unable to connect to Google Cloud SQL using PDO

I'm a PHP newbie. I'm trying to connect to GAE using PHP and PDO. I have tried connecting using mysql_connect() and mysql_select_db(). I have been successful. However when I try to connect using PDO, I get an error
Connection failed: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
My code is as follows:
define('DBH_SOCKET', '/cloudsql/****:****');
define('DBH_NAME', 'wordpress_db');
define('DBH_USER', 'censored');
define('DBH_PASSWORD', 'censored');
$pdo_conn = "mysql:unix_socket=".DBH_SOCKET.";dbname=".DBH_NAME.";charset=utf8";
try {
$dbconn = new PDO($pdo_conn, DBH_USER, DBH_PASSWORD);
$dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
echo "<br> var is ".$pdo_conn;
/*$conn = mysql_connect(DBH_SOCKET, DBH_USER, DBH_PASSWORD);
if (!$conn)
die('Connect error ('.mysql_error());
$db_selected = mysql_select_db(DBH_NAME, $conn);
if(!$db_selected)
die('Cant use db: '.mysql_error());*/
What am I doing wrong? I have looked at some tutorials on the net like, http://webandphp.com/WorkingwithPHPontheGoogleCloudPlatform-166942. They use PHPStorm and a JDBC driver. However, there is no mention of using a JDBC driver in the official google tutorials.
When connecting using PDO locally, you'll need to use a connection string that matches your local MySQL server settings, e.g. "mysql:host=localhost;dbname={db name};charset=utf8".
When running on GAE, your current connection string should work fine. The fact that you're getting the error message "Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?" suggests that you might have missed the "unix_socket=/cloudsql/{project id}:{instance id}" part.

Categories