PDO could not find driver - php

I'm using PHP Version 5.6.25 that was installed via WAMPP. I'm having issues connecting to my database on mySQL DB:
$server = 'jdbc:sqlserver://DB-1\POWERPIVOT;databaseName=SBV_Foldio';
$user = 'sa';
$pass = 'host';
I had check PDO extension and already install
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO is unavailable<br/>';
}
elseif (defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO is available<br/>';
}
PDO is available
but when i run connection
try {
$dbh = new PDO($server, $user, $pass);
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
}
it failed
Error!: could not find driver

The problem is that you do not have the PDO_ODBC module installed right now. PDO seems to be properly configured & installed. See http://php.net/manual/en/ref.pdo-odbc.php for more info.
You are obviously running windows, so:
On Windows, php_pdo_odbc.dll has to be enabled as extension in php.ini. It is linked against the Windows ODBC Driver Manager so that PHP can connect to any database cataloged as a System DSN, and is the recommended driver for connecting to Microsoft SQL Server databases.

Related

Could not find driver when connect PHP to SQL SERVER

I want to connect to SQL Server at another pc with php, when i try to connect it says
Connection error: could not find driver
What I have tried is add extension in php.ini.
;extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll
extension=php_sqlsrv_72_ts_x64.dll
my PHP version is: 7.2.3
SQL SERVER: SQL Server 2014
OS PC: Windows Server 2012 R2 (another pc)
here's my code:
<?php
$host = "192.168.3.126";
$db_name = "dbo";
$username = "david";
$password = "david";
try {
//$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$db = new PDO("sqlsrv:Server={$host};Database={$db_name}", $username, $password);
}
catch(PDOException $exception){ echo "Connection error: " . $exception->getMessage();}
?>
NB: I have download Microsoft Drivers 5.6 for PHP for SQL Server at here, I have download install the ODBC Driver 17 at my own pc
I have tried this reference but it doesn't work PHP Sql Server PDOException:could not find driver

PHP PDO + PostgreSQL Connection Error: Could not find Driver

There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.
I'm receiving the following error when trying to connect to my database via PDO:
Connection error: could not find driver
1. I've ensured that Apache is linked to my homebrew PHP.
$ which php
/usr/local/bin/php
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
The pdo_pgsql module shows up in php_info
3. My database connection is:
<?php
class Database
{
public $conn;
public function getConnection()
{
try {
$this->conn = new PDO("postgres://$user:$pass#$host:$port/$db_name");
print_r($this->conn);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
exit();
}
return $this->conn;
}
}
I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard
4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:
#listen_addresses = '*'
I'm still not having any luck and am looking at some guidance in this head scratcher.
As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
Install it if it is not
# yum install php-pgsql
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
Install PostgreSQL (but I think you already have this installed and
configured)
# yum install postgresql-server postgresql-contrib
Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
After
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Restart postgresql service
# service postgresql restart
Install PHP and PDO connector
# yum install php php-pgsql
Here is an example of PHP script I used to test connection:
<?php
// Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";
$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the $dbname database successfully!";
echo "\n";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
And the output:
# php pdo_test.php
Connected to the masterdb database successfully!

Connection failed: could not find driver, PHP7.1 SqlServer, Windows Server 2012R2, IIS

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!

This extension requires the Microsoft ODBC Driver 11 for SQL Server to communicate with SQL Server

ALready downloaded the sqlsrv on microsoft...
and on my phpinfo()
enabled on php.ini on both C:\wamp\bin\apache\apache2.4.9\bin and C:\wamp\bin\php\php5.5.12
and still got error like this.
Failed to get DB handle: SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver 11 for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712
and my code is
try {
$dbh = new PDO ("sqlsrv:Server=$host;Database=$db","$user","$pass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select top 5 from teams");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
Apart of the pdo_sqlsrv extension, you also need to have the ODBC 11 driver installed on your machine.
You can get it at one of these locations:
https://www.microsoft.com/en-us/download/details.aspx?id=36434
https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

Using PDO to connect to Access database (.accdb)

I am having difficulties with connecting to an Access database (specifically an Access 2013 database with an .accdb extension). Here is the code I'm trying to run:
$dbName = $_SERVER["DOCUMENT_ROOT"] . "/test/testdb.accdb";
echo $dbName."<br />";
if (!file_exists($dbName)) {
die("Could not find database file.<br />".$dbName);
}
try {
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};;Dbq=$dbName");
} catch(PDOException $e) {
echo "Error: ".$e->getMessage()."<br />";
}
I have made sure that the testdb file exists in the correct folder, but when I try to create the new PDO, I am getting the following error: "could not find driver".
Now, I have gone back to my ini settings and confirmed that I have the following extensions selected:
php_curl
php_gd2
php_mbstring
php_mssql
php_mysql
php_mysqli
php_pdo_mssql
php_pdo_mysql
php_pdo_sqlite
I am using WAMP version 2.4 with PHP 5.4.16 on a Windows 7 machine. I would appreciate any and all help I can get.
You'll have to install (if your distro has one) or compile the pdo-odbc generic driver: http://php.net/manual/en/ref.pdo-odbc.php

Categories