Connecting to a MS Access database - php

Someone asked me to do some research to achieve the following result, maybe you guys can help me out an suggest me some tips.
We have a local server and there are different 'jobs' on it ( in a MS Access ) Database.
Now we want to create a platform where other users can check their "job status" with different parameters ( field names etc ) from the MS Access database.
I am looking for the best practice, how to connect to this database from everywhere in the world. I already did some research and found the following links:
http://phpmaster.com/using-an-access-database-with-php/
http://www.php.net/manual/en/function.odbc-connect.php
How can I connect between a web-application and that MS Access database
Can I update it realtime
What kind of protocol does the servers has to support to intereact with a website?
Currently the server can be reached from outside via a VPN connection.
A piece of code I've already tried :
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=Custos_TAB.accdb;Uid=Admin");
}catch(PDOException $e){
echo $e->getMessage();
}
I received the following error : "could not find driver".

Try this, assuming you have the PDO odbc driver installed and enabled on the web server.
$user='Admin';
$password='';
$mdbFilename="Custos_TAB.accdb";
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $user, $password);
$sql="SELECT * FROM [tblHere]";
$rs=odbc_exec($connection,$sql);
//do stuff here
odbc_close($connection);
or
$user='Admin';
$password='';
/*if you have a path here such as c:\db\Custos_TAB.accdb, make sure to use double
backslashes, (i.e "c:\\db\\Custos_TAB.accdb")*/
$mdbFilename="Custos_TAB.accdb";
$conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename;Uid=$user='Admin';Pwd=$password;");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Stop running services first (if there's any)
Open the php.ini file and uncomment the extension for php_pdo_odbc
Start services again (if needed)
Make sure you correctly locate your database in Dbq=Custos_TAB.accdb
//Just an example in my case
Dbq=C:\Users\Server\Documents\Db1.accdb

Related

How to set up an ODBC for MS Access 2016?

I am fairly new to databases and also my English is very bad so don't mind my silly mistakes. I am using:
MS Access 2016 to create a local database named as tc.accdb
XAMPP to provide local host connectivity
this php code to check whether local connectivity is achieved or not:
<?php
$con = odbc_connect( "tc" , "" , "" );
if($con)
{
echo "Connected";
}
else
{
echo "Failed" ;
}
?>
But I just cannot set up Microsoft Access driver for my database. Under administrator tools >.....> System DSN, there exist Microsoft Access driver for (*.mdb) .But since I'm using Access 2016, my database has (.accdb) extension.
I've also tried saving my database as (database.mdb) to avoid extension confilicts but I've used some Office16-specific features so it can't be saved as a database with (.mdb) extension.
Note
Using (.mdb) connectivity with my (.accdb) database just didn't do the job as when I try to run the check code it gives me following error.
try this
$connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
'Dbq=C:\\Users\\Igor\\Desktop\\example.accdb;';
$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
good luck

Using PDO's odbc to connect to a MSSQL server instance with Windows Authentication

I'm trying to connect to a MSSQL database using PDO with odbc. I'm aware that there's a package SqlSrv (but for some reason that package (.dll) won't load properly). So I found some documentation arguing it's also possible with PDO. In my PHP.ini I've enabled the extension php_pdo_odbc.dll and it loads fine.
My connection string looks like this:
$conn = new PDO(
'odbc:
Driver=SQL Server;
Server=MyServer\MyInstance;
Database=MyDatabaseName;
Trusted Connection=Yes;',
'MyWindowsUserName',
'MyWindowsPassword'
);
I've tried various properties (for example by prepending the domain to the username, switching with the authentication options User Id, UID, Password, PWD and Trusted Connection) but I keep getting the message
SQLSTATE[28000] SQLDriverConnect: 18456 [Microsoft][ODBC SQL Server
Driver][SQL Server]Login failed for user 'MyWindowsUserName'.
Any suggestions on how to connect to the database with my Windows Account? (that's the only way for me to connect to the database)
Try removing the username & password
$conn = new PDO(
'odbc:
Driver=SQL Server;
Server=MyServer\MyInstance;
Database=MyDatabaseName;
Trusted Connection=Yes;'
);
I have authenticated by Windows with following PHP statement:
This is my code:
$ Conn = new PDO ("odbc: Driver = {SQL Server}; Server=JAMILI-PC\SQLEXPRESS; null; null");
I am using Windows 2008.
I hope it solves your problem.

Having trouble with ODBC Driver for Access database

I have created an intranet php site using WampServer on my home test environment where everything works fine. I then tried to install it on the production server, again with WampServer installed, but when I try to connect to the database, I get an error:
ERROR: SQL STATE[IM002] SQLDriverConnect:0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.
I have the php_pdo_odbc extension enabled.
Here is the code I'm having trouble with:
$dbName2013 = $_SERVER["DOCUMENT_ROOT"] . "/Ridley/RLCompRepair.accdb";
try {
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};;Dbq=$dbName2013");
}
catch (Exception $e) {
echo "ERROR: ".$e->getMessage();
}
Any and all help would be greatly appreciated.
Chris
You are trying to connect with DNS-less database.
create a Windows ODBC DNS
In Windows go to "Administrative Tools" and Select "Data Sources"
Go to File DSN select Add
select Microsoft Access Driver (*.mdb)
Select Next, type in a Data source Name
select "Next" then Finish
You should be in ODBC Microsoft Access Setup
select "Select" navigate to your .mdb and select it.
select "OK"
In your PHP try this:
$connect = odbc_connect("[data source name given]", "user", "password");
$connect = odbc_connect("myaccess", "", "");
if($connect === false){echo "did not work<br>";exit}
echo "success";

Connect MS Access DB In Linux Using PHP

i have site hosted on bluehost.com (Linux Server), i have use case that i have to Export some Specific Data Export to MS Access File,
I have following code to Connect with MS Access File:
$dbh = null;
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_path", $user_name, $password, $db_info);
}catch(PDOException $e){
echo $e->getMessage();
}
return $dbh;
But when i run the above code on Hosting server than i got error
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
Later after some searching i changed the code of DB connection to
try{
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_path", ACCESS_DB_USERNAME, ACCESS_DB_PASSWORD);
}catch(Exception $ex){
echo $ex->getMessage();
}
in above code i did not get any exception but $conn is NULL, both code snippet working fine on my local Windows machine,
Can you people help me in fixing issue?? i want to make connection and want to run INSERT INTO statement on MS Access DB.
The docs for odbc_connect say it returns one of two things.
An ODBC connection.
FALSE on error.
Your code should look for FALSE, not for an exception.
The docs also have examples of several different kinds of connections. But I think the chances that Bluehost installs Microsoft Access drivers on all their Linux servers are less than zero.

What will be connection string to Access database file with PHP

I installed WAMP, I have access database file in project folder, but don't have installed Access on my computer.
Can I read and update Access file with PHP even I don't have installed Access?
And what will be connection string to Access database file?
I really need help with this.
All you need is the PHP api for ODBC.
Here is the example from the docs itself:
<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>
// Microsoft Access
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver.
Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
$dsn='database.accdb';
$username='';
$password='';
$connect=odbc_connect($dsn, $username, $password);
I'v found this link with a tutorial on how to do it. Be careful that things work differently in windows and UNIX environment, but since you are using a WAMP you should have no problems
<?php
$db = $_SERVER["DOCUMENT_ROOT"] ."/AccessDatabase/reg.accdb"; //AccessDatabase is folder in htdocs where the database is store
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 reg"; //reg is table name
$rs = $dbNew->query($sql);
while($result = $rs->fetch())
{
echo $result[0].": ".$result[1].": ".$result[2]."<br />";
}
?>
If u got error like pdo ODBC Drivers not installed
just go to php.ini and find extension = pdo_ODBC Driver and remove the comment(;)
after that restart the apache

Categories