Using PDO to connect to Access database (.accdb) - php

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

Related

PHP Sql Server PDOException:could not find driver

My server is a Windows 2008 server. PHP Version 7.2.7 is installed and running. Sql Server 11 (64 bit) is installed and is working (there is a couple asp.net apps running and already using that database)
I downloaded the PHP Sql Server Drivers from Microsofts website and placed the .dll files in the PHP ext directory.
In my PHP.ini I added:extension=php_pdo_sqlsrv_7_nts_x64
In my .php file I am using to test my db connection I have:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
I am getting:
PHP Fatal error: Uncaught PDOException: could not find driver in D:\Inetpub\wwwroot\TechStory2\DBtest.php:7 (Line 7 is the $SqlServerCon line).
What did I do wrong? and What do I need to do to get a connection to Sql Server?
I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.
For future reference for anyone else with this or a similar issue...
It can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=56567 (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.
In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
More info can be found at https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.
PDO connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
sqlsrv_connect connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
As commentators said - adding .dll at the end of extension= config line should be good start with your issue.
I can also see that you're trying to load NTS version of extension (NTS stands for non thread safe). Are you sure that you're going to load right version of extension? Please check if you're running PHP as NTS runtime or not - you can check it with phpinfo();.

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!

PHP can't connect to SQL Server

I have been thrown into the world of PHP, and I'm having a bit of an issue connecting to my SQL server. I'm using the following code to try to connect. When it hits the SQLsrv_connect command, it just seems to stop processing. No error, it just stops loading.
function testConnection()
{
$returnable = "TEST";
echo 'Current PHP version: ' . phpversion();
$connectionInfo = array("Database"=>"MyDatabase", "UID"=>"MyLogin", "PWD"=>"MyPassword");
$conn = sqlsrv_connect("MyServer",$connectionInfo);
if ($conn) {
echo "Connection Established.<br />";
} else {
echo "Something went wrong while connecting to MSSQL.<br />";
}
return $returnable;
}
Any idea on what I might be missing? I tried some very old syntax for version 5, and I got the same issue. I am trying to connect to sql server 2008.
Thanks
First, check if you have installed the sql_srv extension for PHP.
Probably this extension is not installed/loaded by you php.ini file.
For Windows
Download proper driver from SQL Driver for MSSQL extract and copy into you php installation directory. Then edit php.ini file and add in extensions path your extension. (For 99% you should copy NTS sql_srv version) also don't forget add sql_srv_pdo extension.
For Ubuntu/Linux
You can try install sql_srv and pdo_sql_srv by pecl (if is installed)
pecl install sqlsrv pdo_sqlsrv.

PDO could not find driver

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.

Connecting to microsoft sql server using php

I am trying to connect to a mssql server using php. The sql server is on a different machine in the network and I have XAMPP installed in my machine. I don't have microsoft sql installed in my server.
I have downloaded the sqlsrv drivers for PHP and then in my php.ini file added the extension extension=php_pdo_sqlsrv_55_ts.dll under windows extension.
Added the php_pdo_sqlsrv_55_ts.dll file inside the php\ext\ folder of my XAMPP installation.
After restarting apache phpinfo(); shows that the sqlsrv driver is enabled
PDO support enabled
PDO drivers mysql, sqlite, sqlsrv
This is my code
<?php
$serverName = "192.168.100.102, 1433";
$connectionInfo = array( "Database"=>"ATP", "UID"=>"raihan", "PWD"=>"temp123#");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
But on executing I get this error
Fatal error: Call to undefined function sqlsrv_connect() in
D:\xampp\htdocs\database\index.php on line 4
Why this error? And how do I go about connecting to mssql server using php?
php\etc\ sounds like wrong folder, try put in php\ext\ folder
Note extension=php_pdo_sqlsrv_55_ts.dll is used by PDO class
Go to https://www.microsoft.com/en-us/download/details.aspx?id=20098
Select download by your PHP version:
Select SQLSRV30.EXE if use SQL Server 2005
Select SQLSRV31.EXE or SQLSRV31.EXE if use SQL Server 2008
After download, extract dlls
If you use php with ThreadSafe (apache2handler -- more probable) copy php_sqlsrv_55_ts.dll fot ext folder
For use sqlsrv uncomment (or put) this line in php.ini extension=php_sqlsrv_55_ts.dll (or extension=php_sqlsrv_55_nts.dll for no-thread-safe)

Categories