Cannot connect to MS Access DB with PHP : SQLSTATE[IM002] - php

I am trying to get data from a MS Access DB (.mdb) but I'm getting:
échec de connexion: SQLSTATE[IM002] SQLDriverConnect: 0
[Microsoft][Gestionnaire de pilotes ODBC] Source de données
introuvable et nom de pilote non spécifié.
English:
connection failure: SQLSTATE [IM002] SQLDriverConnect: 0 [Microsoft]
[ODBC driver manager] data source not found and driver name
unspecified.
I have : Office 365 Business 32-bit
installed. Already installed the latest Access Database Engine 32-bit. Uncommented " extension=pdo_odbc " in php.ini.
Added the "Microsoft Access Driver" to the list of drivers.
$dbName = 'C:\wamp64\www\Test\Workgroup.mdb';
if (!file_exists($dbName))
{
die("Could not find database file.");
}
else
{
try{
//$dbName = 'Workgroup.mdb';
$cnx = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; Dbq=$dbName; Uid=; Pwd=;");
echo "connected successfully";
return $cnx;
}
catch(Exception $e){
die ("failed to connect: " . $e->getMessage());
}
}

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

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

WAMP php ODBC Connection 'Warning'

I have a problem with ODBC in php (WAMP Server 2.5, PHP 5.5.12).
When I try to run:
$conn = odbc_connect("odbc_dsn", "user", "pwd");
if ($conn)
{
echo "Connection established.";
}
else
{
exit("Connection could not be established.");
}
I get a Warning:
Warning: odbc_connect(): in C:\wamp\www\Concepts\index.php on line 29
Connection could not be established.
If I change the "odbc_dsn" to something else (that doesn't exist) like "fdbasdf" then I get:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager]
Der Datenquellenname wurde nicht gefunden, und es wurde kein Standardtreiber angegeben,
SQL state IM002 in SQLConnect in C:\wamp\www\Concepts\index.php on line
Connection could not be established.
(Translated)
The Data Source could not be found, and no default driver is given.
This tells me that the first execution was able to find the db, but did not open it... Why?
Is this another config thing? Is it trying to execute Client-Side? What can I do?
You have to specify the Host and Connect protocol:
$connect_string = "Driver={SQL Anywhere 12};".
"CommLinks=tcpip(Host=$db_host);".
"ServerName=$db_server_name;".
"DatabaseName=$db_name;";
// Connect to DB
$conn = odbc_connect( $connect_string, $db_user, $db_pass );

How to connect access database ".accdb" with 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

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