PHP connection "Failed to get DB handle: could not find driver" - php

I'm using the following code to try and connect:
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;
}
This is my Error:
Failed to get DB handle: could not find driver
And the DLL Files are already in the php/ext and modified in the php.ini
What else can I do?

There is following of possibility here,
enable mssql in php.ini
;extension=php_mssql.dll
to
extension=php_mssql.dll
try editing your setting at the php.ini mssql.secure_connection must be set to on.
mssql_connect can use secured connection if php is configured ...
go into the php.ini and verify the mssql.secure_connection parameter
[MSSQL]
mssql.secure_connection = On
3.Try this:
Uncomment the following in php.ini by removing the ";"
;extension=php_pgsql.dll
I hope this will help you.

Related

PHP Warning: PHP Startup: Unable to load dynamic library 'php_ibm_db2.dll'

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

Connecting to sql server data with PHP on ubuntu

When trying to connect to my mssql database, I get the error
"SQLSTATE[01002] Adaptive Server connection failed (severity 9)"
Below is the php code I'm running
<?php
try {
$hostname = "hostname.database.windows.net";
$port = 1433;
$dbname = "database-dev";
$username = "dbuser";
$pw = "dbpassword";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
?>
And below is my odbc.ini, odbcinst.ini and freetds.conf, you can see my phpinfo() here ("http://wingedw.com/matiks/connect.php") the driver is set to freetds and the pdo and pdo_dlib modules have been added to php 5, any clues as to why im getting error, im sure the credentials are right.
odbc.ini
[MSSQLServer]
Driver = FreeTDS
Description = Any description
Trace = No
Server = servername
Port = 1433
Database = dbname
wTDS_Verison = 7.1
odbcinst.ini
[FreeTDS]
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
freetds.conf
[global]
# TDS protocol version
; tds version = 7.1
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 100
; connect timeout = 100
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 7.1
# A typical Microsoft server
[MSSQLServer]
host = servername
port = 1433
tds version = 7.1
Turns out everything was configured correctly, the issue was using dblib over odbc, see code below.
try {
$hostname = "hostname.database.windows.net";
$port = 1433;
$dbName = "databasename";
$dbuser = "dbuser#hostname";
$dbpass = "password";
$dbh = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName,
$dbuser, $dbpass);
//echo "COnnected";
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
2021 - BACK TO THE FUTURE:
I know that this post is old, but my reply can point a desperate soul in the right direction:
You are using odbc and the command "isql" works just fine
You are using FreeTDS and the command "tsql" works just fine
Trouble is running the php scripts that have calls to odbc via FreeTDS -> keeps giving errors
Kindly read:
https://www.linuxquestions.org/questions/blog/tix-592494/freettds-libiodbc-iodbc-php-7-4-21-on-slackware-current-5-13-5-38621/
Cheers happy souls!

PDO Exception error while connecting to microsoft azure

i have problem connecting to azure on my mac. I've read many articles here and added multiple extension to my php.ini file but nothing seems to work. I just want to simply run this code and connect to database. All the variables in the code have the actual values are correct.
At the moment it gives me following error : "PDOException Object ( [message:protected] => could not find driver".
i've looked throuh mutiple articles on this issue, and added extensions to php.ini. i've pasted all of the below for people to check. i also now that i have PDO attached to my server. Unforunately, i cant post the screemshot here, but my pdo_mysql, pdo_pgsql, pdo_sqlite in the phpinfo() call.
i would really appreciate any info and help on this matter. thank you!
$server = "tcp:*********.database.windows.net,1433";
$user = "jus***#********";
$pwd = "password";
$db = "testdb";
try
{
$conn = new PDO( "sqlsrv:Server= $server ; Database = $db ", $user, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die(print_r($e));
}
;Extensions
;extension=apcu.so
extension=imap.so
extension=yaz.so
extension=mcrypt.so
extension=gettext.so
extension=pgsql.so
extension=pdo_pgsql.so
extension=pdo_mysql.so
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_pdo.dll
extension=php_pdo_firebird.dll
extension=php_pdo_informix.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_oci8.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=imagick.so
In addition to the extensions that you have enabled in php.ini you will need to install:
Microsoft Drivers 3.0 for PHP for SQL Server
You have to also make sure you are not using the wrong php.ini
Avoid these spaces in your connection string, and if you anything you should catch PDOException.
$server = "tcp:*********.database.windows.net,1433";
$user = "jus***#********";
$pwd = "password";
$db = "testdb";
$dsn = "sqlsrv:Server=$server;Database=$db";
try {
$conn = new PDO($dsn, $user, $pwd );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
die(print_r($e));
}

could not find driver php&pdo [duplicate]

i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.

PDO: Could not find driver php/mysql

i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.

Categories