PHP PDO + PostgreSQL Connection Error: Could not find Driver - php

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!

Related

Howto connect with PHP to a MS SQL Server within a Docker setup?

I am trying to connect to a MS SQL server with PHP in a Docker environment.
I followed this instructions for my Dockerfile:
FROM php:7.3.10-apache
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
The following two lines are added to the php.ini:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
The phpinfo() shows this:
Now I am trying to connect to the MS SQL Server (which is not part of the docker environment):
$server = 'MyServer';
$database = 'MyDB';
$username = 'MyUser';
$password = 'MyPassword';
# Connect
try {
$conn = new PDO("sqlsrv:server=$server;Database=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Error connecting to SQL Server: ".$e->getMessage());
}
But I am getting the following error:
Error connecting to SQL Server: SQLSTATE[IMSSP]: This extension
requires the Microsoft ODBC Driver for SQL Server to communicate with
SQL Server. Access the following URL to download the ODBC Driver for
SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712
Can someone point me in the right direction? What exactly do I need to do?

Could not connect to the database mytable: could not find driver

I had the php version 5 but now i upgraded to php 7 and i am getting problems that a didn't had with the older version.
One that i'm still trying to solve is this:
Could not connect to the database mydatabase :could not find driver (this appear when i run my script).
I tryed to open phpmyadmin and also appeared an error:
The mysqli extension is missing. Please check your PHP configuration.
After a lot of search i still can find a solution, is this related to the version of my mysql on xampp? It need to be upgraded too?
Apache version: 2.4
mysqlnd 5.0.12
********************** EDIT*****************
Here is the code that makes the connection to database:
function connection()
{
$host = 'localhost';
$dbname = 'mydatabase';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname;", $username, $password);
$conn->exec("SET CHARACTER SET utf8");
// echo "Connected to $dbname at $host successfully.";
}
catch(PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
echo "Line: " . __LINE__;
}
return $conn;
} //connection
Either install the mysqli driver on your server or use PDO, if that's available. Use this code to find out more information about the installed drivers:
<?php phpinfo() ?>
Go to your php.ini file and uncomment this line
extension=mysqli
Then restart your local server
This was how i solved the problem:
1º rename php.ini-developer to php.ini
2º add extensions:
extension_dir = "D:\Programs\xampp\php\ext"
extension=php_mysqli.dll
extension=pdo_mysql
extension=mbstring
extension=php_mbstring.dll
Working like a charm now!

Using PHP MSSQL PDO with Plesk

My current Website Control Panel is PLESK Parallels, and i'm trying to use PDO MSSQL on my website.
I've searched the internet and i'm aware that the PDO dll needs to be in my php.ini file and such, but I keep reading different ways to do it all the time, using commands like YUM and i'm getting confused.
Can someone possibly, in plain, simple black and white instructions, explain the process so I can simply run code like this
try {
$hostname = "myhostname";
$port = myportnumber;
$dbname = "databasename";
$username = "user";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
When I load my page where this command is located i get the error message
'Failed to get DB handle: could not find driver'
Thanks
Example of installing pdo_dblib on CentOS-like OS for Plesk PHP 5.6:
# yum install plesk-php56-devel
# /opt/plesk/php/5.6/bin/pecl download pdo_dblib
# tar -xzf PDO_DBLIB-1.0.tgz
# cd PDO_DBLIB-1.0/
# /opt/plesk/php/5.6/bin/phpize
# ./configure --with-php-config=/opt/plesk/php/5.6/bin/php-config --with-pdo-dblib=./freetds
# vim pdo_dblib.c
On line #37 replace:
function_entry pdo_dblib_functions[] = {
with:
zend_function_entry pdo_dblib_functions[] = {
Save file and:
# make
# make install
you should see something like
Installing shared extensions: /opt/plesk/php/5.6/lib64/php/modules/
Enable extension:
# echo "extension=pdo_dblib.so" >> /opt/plesk/php/5.6/etc/php.d/pdo_dblib.ini
Verify:
# /opt/plesk/php/5.6/bin/php -m | grep pdo_dblib
pdo_dblib
Now sites in Plesk with PHP handler 5.6 will work with:
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
Pay attention for :
PHP DBlib PDO Issue
How to connect MSSQL from PHP 7, Plesk 12.5 installed on CentOS 7
PHP PDO works in OOPS .
Hence in PHP PDO first you must create a database handler using PDO class which is already defined in PHP Library. which will work as object for your database queries.
When you connect your database using PDO Class than you are ready to play with your queries with the database handler which you got during PDO connection.
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
Here $dbh will works as database handler object for MySQL Queries Operations.
I have a personal blog for PHP PDO here . Just go to reference and take a look how queries are working . Finally i would like to say it's very simple and secure.

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

MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2)

I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel

Categories