accdb and PDO php connection error - php

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

Related

Microsoft Access from php with odbc Error : (07002)

I'm trying to run my first php script that is connecting to my MS Access .accdb database.
Here's my code:
<?php
$dbName = "G:/xampp/htdocs/1.accdb";
$nazwa_produktu = (string)$_POST["nazwa_produktu"];
$cena = (int)$_POST["cena"];
$ilosc = (int)$_POST["ilosc"];
$kategoria = (string)$_POST["kategoria"];
if (!file_exists($dbName)) {
die("Access database file not found !");
}
$conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=$dbName; Uid=; Pwd=;");
$sql = "INSERT INTO [produkty] (nazwa_produktu, cena, ilosc, kategoria) VALUES ($nazwa_produktu, $cena, $ilosc, $kategoria)";
if($conn->query($sql))
{
echo 'success';
}else{
$db_err = $conn->errorInfo();
echo 'Error : ('. $db_err[0] .') -- ' . $db_err[2];
}
I'm still getting:
Error : (07002) -- [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2. (SQLExecute[-3010] at ext\pdo_odbc\odbc_stmt.c:258)
Probably it has something to do with Uid and Pwd in $conn but I really have zero idea what can I do more, because when I try run this without this parameters there's still something wrong.
Try specifying the Uid as Admin, per this reference, i.e.:
$conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=$dbName; Uid=Admin; Pwd=;");

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

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';
?>

PHP and MS access not working

$dbName = "C:\path\folder";
$user=""; $password="";
if(!file_exists($dbName)) {
die("Could not find database file.");
} echo"connecting...";
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};
DBQ=$dbName; $user, $password");
the above is my connection string code for .mdb, I am running IIS7 on win7. My php page runs perfectly, but when I include or put the connection string above my page, it loads only until the
echo"connecting...";
and don't load anything under the string ...
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=$dbName; $user, $password");
So I transferred the $db = new PDO()... in the footer area and everything loads above this string.
Can anyone help me with this crazy problem?
From the Manual, the connection string should be
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", $user, $password);
In your case, it should be
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};
Dbq=$dbName" $user, $password);
Thanks for your help Friend. I found a working code here. Thanks again, 'till next problem. ^_^
Using PHP to query a MDB file, and return JSON
PDO Query Database ODBC
Actually, I have this now.
$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)}; DBQ=Northwind.mdb','','');
if($conn){
echo "Database Connected!";
} else echo "Database Disconnected.";
Hope this will helps those who are new to php and MS Access.
Thanks.

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();
}

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