getting error in connecting with access database in pdo - php

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.

Related

Unable connect to Access database (no password) with php using PDO [duplicate]

This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 2 years ago.
I need help and hope you can help me!
I use the below code but nothing displays.
Additional check with phpinfo shows that everything on the server is in place.
The db is at the right spot and there is no error for file not found, so the problem lies in the code from $dbNew and on.
What can I do/try?
Thanks in advance.
<?php
$db = "vw.mdb";
if (!file_exists($db))
{
die("No database file.");
}
$dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
$sql = "select * from vw";
$rs = $dbNew->query($sql);
while($result = $rs->fetch())
{
echo $result[0].": ".$result[1]."<br />";
}
?>
Addition A: I have to add that I am new to PHP, forced to rewrite my Classic ASP code in PHP in order to risk being without any homepage beginning the day ASP is taken off Windows.
You should catch the error and view it:
try {
$dbh = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
} catch (PDOException $error) {
echo 'Connection failed: ' . $error->getMessage();
}

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 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

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

PHP and MS Access

How can we connect a PHP script to MS Access (.mdb) file?
I tried by including following PHP code:
$db_path = $_SERVER['DOCUMENT_ROOT'] . '\WebUpdate\\' . $file_name . '.mdb';
$cfg_dsn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" . $db_path;
$odbcconnect = odbc_connect($cfg_dsn, '', '');
But it failed and I received following error message:
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\web\WebUpdate\index.php on line 41
Here's a sample for a connect and a simple select...
<?php
$db_conn = new COM("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("./Northwind.mdb").";";
$db_conn->open($connstr);
$rS = $db_conn->execute("SELECT * FROM Employees");
$f1 = $rS->Fields(0);
$f2 = $rS->Fields(1);
while (!$rS->EOF)
{
print $f1->value." ".$f2->value."<br />\n";
$rS->MoveNext();
}
$rS->Close();
$db_conn->Close();
?>
In the filename, I'm looking at '\WebUpdate\' - it looks like you have one backslash at the beginning at two at the end. Are you maybe missing a backslash at the beginning?
$db_path = $_SERVER['DOCUMENT_ROOT'] . '\WebUpdate\\' . $file_name . '.mdb';
replace the backslashes with slashes use . '/WebUpdate/' .
it looks like a problem with the path seperators. ISTR that you have to pass backslashes not forward slashes
The following works for me - with an MDB file in the webroot called db4
$defdir = str_replace("/", "\\", $_SERVER["DOCUMENT_ROOT"]);
$dbq = $defdir . "\\db4.mdb";
if (!file_exists($dbq)) { die("Database file $dbq does not exist"); }
$dsn = "DRIVER=Microsoft Access Driver (*.mdb);UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL=MS Access;DriverId=25;DefaultDir=$defdir;DBQ=$dbq";
$odbc_conn = odbc_connect($dsn,"","")
or die("Could not connect to Access database $dsn");
I'm not certain if this is a violation of best practices or security, but I would like to throw out this suggestion:
set up an ODBC connection and include the database's password in the odbc advance settings.
give the odbc conn a DSN name then save.
in your code, just set up the connection like:
try {
$conn = #odbc_connect("DSNName", "", "", "SQL_CUR_USE_ODBC");
// un and pw parameters are passed as empty strings since the DSN
// has knowledge of the password already.
// 4th parameter is optional
$exec = #odbc_exec($conn, $insert) or die ("exec error");
echo "success!";
}
catch (Exception $e) {
echo $e->getMessage();
} // end try catch

Categories