MS Access PHP Connection using PDO "could not find driver" error - php

I am unable to connect to my Access database with the following code. I receive a "could not find driver" error. Can anyone offer a solution?
<?php
$mdbFileName = realpath('Project1.accdb');
try {
$dbh = odbc_connect("Driver={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=$mdbFileName",'','');
if (!$dbh)
echo 'Failed3';
else
echo 'Success3';
}
catch (PDOException $e)
{
echo $e->getMessage();
}
odbc_close($dbh);
?>

My solution was to use a COM object and an OLEDB connection instead of PDO and an ODBC connection:
<?php
$dbh = new COM('ADODB.Connection') or die('Cannot start ADO');
$dbh->Open('Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Project1.accdb; Persist Security Info=False;');
if (!$dbh)
echo 'Failed3';
else
echo 'Success3';
?>

Related

Fedora php pdo not able to connect sql SQLSTATE[HY000] [2002] Connection refused

installed php7.x while connecting from fedora with the following detail:
try {
$conn = new PDO("sqlsrv:Server=$dbHosts;Database=$bNames", $dbUsers, $dbPasss);
//Error for this ....ould not find driver
//$conn = new PDO("odbc:Driver={SQL Server};Server=$dbHosts;Database=$dbNames", $dbUsers, $dbPasss);
//Error for this....SQLSTATE[01000] SQLDriverConnect: 0 [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
i have install all(odbc, mysql) with fedora
where i may have mistake same application with same connection class to same database with credentials works in xampp
regards

php connect to mysqlworkbench localhost

I have a mac with OSX 10.8.4. I have installed my localhost and it works just fine. I have made a php script, from where I would like to connect MySQL workbench database to. My apache tomcat server runs, and also mysql on the computer, and I use XAMPP. This is my code:
<?php
// Establish connection to DB using PDO
try {
$pdo = new PDO('127.0.0.1:3306', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
echo "Connected!";
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo "Connection failed";
exit();
}
I have tried this script to connect to a remote mysql server, where it works fine, but I cannot use it for my localhost. I also tried just to put in localhost in new PDO, but still the same. Does anybody have a clue to what is wrong?
Best Regards
Mads
You'll have an easier time knowing what's not working if you echo the exception being thrown.
Your code
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo "Connection failed";
}
doesn't actually print the exception! Try this instead:
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo $error;
}
That will at least give you some helpful debugging info.

How to connect user name password protected ms access 2000 database (.*mdb) using php

Hi guys i want to connect USERNAME,PASSWORD protected MS ACCESS 2000 (.*mdb) database using PHP, ODBC or any other database connection. But given below code show WARNING and doesn't connect to the database. please anyone help.
CODE 1
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$user = 'test';
$password ='123';
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename;",$user,$password);
if($connection)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
WARNING MESSAGE
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You do not have the necessary permissions to use the '(unknown)' object. Have your system administrator or the person who created this object establish the appropriate permissions for you., SQL state 42000 in SQLConnect in C:\xampp\htdocs\punch\test.php on line 6
Output -> Connection Failed
CODE 2
I tried given below code also. there are no any ERRORS or WARNING MESSAGE but not connect to the access database.
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$systemDbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\SYSTEM.MDW";
$user = 'test';
$password ='123';
$adoCon = new COM("ADODB.Connection") or die("Cannot start ADO");
$connection= $adoCon->Open("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename; SystemDB=$systemDbFilename;Uid=$user; Pwd=$password;");
if(($connection->State) > 0)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
Output -> Connection Failed
CORRECT ANSWER
Finally I found the answer. I tried PDO to connect ODBC.
WORKING CODE
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Door\DB\IMSDB.mdb;SystemDB=D:\Door\DB\SYSTEM.MDW;Uid=test;Pwd=123;");
if($dbh)
{
echo "Connection Success";
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}

accdb and PDO php connection error

I'm trying to create a simple connection using PDO (on localhost - xampp):
<?php
try{
// Connect
//$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=.\db\icr.accdb;Uid=Admin");
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\xampp\htdocs\bd\db\icr.accdb;Uid=Admin");
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM Miasta";
foreach ($dbh->query($sql) as $row)
{
print $row['Nazwa'] .' - '. $row['IDWojewództwa'] . '<br />';
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
But then I get this error:
SQLSTATE[HY000] SQLDriverConnect: 63 [Microsoft][Driver ODBC Microsoft Access]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x5f8 Thread 0x1124 DBC 0x3608134 Jet.
any ideas?
Thanks a lot vodich, here is solution:
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\xampp\\htdocs\\bd\\db\\icr.accdb;Uid=Admin");
How about this
PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};
Dbq=C:\xampp\htdocs\bd\db\icr.accdb;Uid=Admin");

PHP PDO error successful no matter what

I have a problem with my database! Here is my code:
<?php
$host = "/homes/49/jc192699/public_html/dbase";
$database = "EduPro.db";
$dbhandle = new PDO("sqlite:".$host.$database);
if (!$dbhandle){
echo "Error connecting to database.\n";
}
else{
echo "<br>";
echo "<br>";
echo "Database connection successful!";
}
mysql_select_db($database);
?>
The problem is that it's saying "Database connection successful!" No matter what I do, if I type the address in wrong, it still says successful, when I renamed the database to a database that doesn't exist, it still says successful. I can't see what the problem is here?
If anybody could help me out it would be GREATLY appreciated!
Thank you!
For starters, the PDO constructor throws an exception if there is an error. It does not return false. Check for errors using
try {
$dbhandle = new PDO("sqlite:".$host.$database);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Secondly, as you are using SQLite, provided your dbase directory is writeable by the script, your connection attempt will create an empty database.
Try this:
<?php
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:/path/to/database.sdb");
}
catch(PDOException $e)
{
/*** real error message prints here ***/
echo $e->getMessage();
}
?>
This is directly taken from here:
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.2

Categories