How to connect access database ".accdb" with php - php

I am try to connect access DB with PHP by using this code-
$file = FILE_PATH."/SRWOSystem.accdb";
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb , *.accdb)};Dbq=$file");
}catch(PDOException $e){
echo $e->getMessage();
}
Getting this error:
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
How we resolve that.please help

Related

getting error in connecting with access database in pdo

I am getting following error while using pdo in php.
My database is in C:xmapp/htdocs and php file is in C:xmapp/htdocs/selftest/
My error :
Error:SQLSTATE[HY000] SQLDriverConnect: -1811 [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
My code:
$database_path = $_SERVER['DOCUMENT_ROOT']."\Database11.mdb";
//echo $database_path; die;
try
{
$conn = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$database_path; Uid=$db_username; Pwd=$db_password;");
$conn->setAttribute("PDO::ATTR_ERRMODE,PDO::ERRMODE_Exception");
$stmt=$conn->prepare("insert into user_details(firstname,lastname,filename,sex,qualification) values('$firstname','$lastname','$filename','$sex','$qualification')");
$stmt->exceute();
}
catch(PDOException $e)
{
echo "Error:".$e->getMessage();
}
$conn=null;
Backslash in string needs to be escaped. try
$database_path = $_SERVER['DOCUMENT_ROOT']."\\Database11.mdb";
or simply use forward slash
$database_path = $_SERVER['DOCUMENT_ROOT']."/Database11.mdb";
as forward slash does not have to be escaped but still works perfectly.
Also check if $_SERVER['DOCUMENT_ROOT'] points to the right location.

connecting to MS access with php when defined password for database

I am trying to connect Ms Access DataBase from php .
My codes like this in config.php
define('DBNAMEACCESS', '\\'."\\xxx.xxx.xxx.xxx\\test\\test.accdb");
define('DBACCESSPASSWORD', 'mypassword');
define('DBACCESSUSERNAME', '');
and in process.php like this:
include './config.php';
if (!file_exists(DBNAMEACCESS)) {
die("Could not find database file.");
}
try{
$dbName=DBNAMEACCESS;
$username=DBACCESSUSERNAME;
$password=DBACCESSPASSWORD;
$dba = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbName",$username, $password);
if ($dba) {
/*......*/
} else
{
die("Could not connect to access database");
}
}
catch (Exception $ex) {
// var_export($ex);
setmessage($ex) ;
}
when the password is defined for access file , I get this error on this line:
My error: odbc_connect(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Cannot open database '(unknown)'. It may not be a database that your application recognizes, or the file may be corrupt., SQL state S1000 in SQLConnect in this line
$dba = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbName",$username, $password);
and when the password is not defined for access file,My codes execute correctly.
The problem was related to the Microsoft Access Database Engine Installation.
I've tried with this and restart my computer.
then worked correctly.

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");

Connecting to MS Access 2007 using ODBC_CONNECT - error in driver?

I have been successful in querying a Microsoft Access 2003 database (.mdb file) and now I am trying to do the same for a Microsft Access 2007. I have tried the following:
if($type[1]=='accdb'){
echo 'accdb';
//2007 Microsoft Access
$connection = odbc_connect("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$mdbFilename;Persist Security Info=False;", $username, $password);
}else{
echo 'mdb';
//2000, 2003 Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $username, $password);
}
However, for an access 2007 database I get this error:
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified, SQL state IM002in SQLConnect
in E:\export.php on line 38
In case anyone is wondering how to do this. This worked for me.
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $username, $password);
}catch(PDOException $e){
echo $e->getMessage();
}
IKIK This is uber grave digging but..
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" . $mdbFilename, $username, $password);
This should work.

Categories