Connecting to ms-access database

hi all

i am trying to connect to ms-access database

but it says “Access database file not found !”

i m using xampp

i have database file in e: directory

<?php
$db_username = ''; //username
$db_password = ''; //password

//path to database file
$database_path = "E://mike.mdb";

//check file exist before we proceed
if (!file_exists($database_path)) {
    die("Access database file not found !");
}

//create a new PDO object
$database = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$database_path; Uid=$db_username; Pwd=$db_password;");

$sql  = "SELECT * FROM Customers";
$result = $database->query($sql);
while ($row = $result->fetch()) {
    echo $row["First Name"];
}
?>

Is that file path correct? Wouldn’t it be a single backslash if it’s in the root directory on E? I could see it translating the direction but would it deal with double slashes?

$database_path = "E:\mike.mdb";

As @droopsnoot has mentioned change your path string to only contain one backslash. I would also check the read/write permissions to the E:/ directory as you may not have access to the directory.

Hi droopsnoot

thanks for the reply

yes it worked fine with double slashes.

Next question i want to ask is

at present my access database file is also on my pc and xampp is also on my pc

But if i want to upload the php file on online webserver and want to connect to access database file on my pc , then what will i have to do ??

will my same php file work on online webserver also and will be able to connect to my database file on my pc ??

vineet

I don’t know how you’d do that - if it is possible, I would imagine it would mean sharing the database file on your PC with the hosted server by opening it up as a share, and configuring that to be able to access your PC. Sounds like a security hole to me, though it might not be. Your Internet connection will almost certainly require a static IP address, do you have one? If it keeps changing you might get away with configuring some kind of Dynamic DNS service to keep it working.

Why would you want to do it that way, though?

Hi droopsnoot

The database file is of desktop accounting software and is in microsoft access format

i want it to sync that file with my online database through php

means i want
when i update prices in the microsoft access format file then i want to my online database to get updated or vice versa
But this microsoft access database file exists in the computer hard drive. its not online.

Can it be done with Setting DSN connection with ODBC on the computer and then querying it through php online ?? any idea if this is possible or not ??

vineet

You would need a desktop application to interface with your online application. So to give you an overview of how those type of applications work and how to go about building one. So you will need to write a desktop application for what OS’s you plan to support. Lets say you wanna support windows only you can then use a language like C# and write a console or GUI(graphical user interface) but c# is not the only language as many can write desktop applications. But C# is easy and works well on windows since its microsoft language. But this desktop app will need to then connect to your access database. So their is a few ways you can manage the data changing such as update fields or querys that check the data against another table or database against another database or export and import. There are many ways you can accomplish the data changing event. But once you have the data that is change you will need to send it to the server. This will need to be done through a restful api that can receive the requests sent from your desktop application.

So things you will also need to manage is making sure there is an internet connection and what to do if there is not with the data that was chnaged as well as prevent the application from erroring out when there is no internet.

Also there is security concerns for the data being sent through the request so you will need to encryt the data and also since it could be sensitive data you mind want to think about authentication so data cannot just be sent in and out with out proper authorization. So now you need to decide if the application user needs to login or will it be a static or dynamic based system as this part can become rather tricky and complex depending on your needs.

There is far more into it but as you can see it sounds simple in your head but the reality of it is these projects are icebergs.

hi jgetner

i dont know how to work and code in C# language

is there any ready made program available that can be used to communicate between these two files ??

vineet

hi jgetner

also i have one more question

i have ODBC software installed on my desktop

see screenshot attached

cant we use this ODBC desktop software to communicate with php server online ??

vineet

The problem you have is that the PHP server at your hosting company needs to connect in to your desktop PC in order to be able to access it. That means two things - you have to open up your PC to accept an incoming connection into your accounting database, and you have to have some way the PHP server can find your desktop PC. The first sounds like quite a security hole, the second means you’ll either have to have a static IP address on your internet connection, or use a dynamic DNS service.

Can’t you move the accounting access file to your web host, and configure your desktop application to use it there? Your hosting company might not be prepared to configure it - if they’re not, there may be a good reason that an incoming connection to an Access database isn’t a good idea. If they will, the down side is you won’t be able to use your accounting system when the net connection is down.

If the data you want to sync is limited to things like prices, a definable list of fields, then the best way you can do it is to write an import and an export routine on both ends so you can transfer the required data. You could write code to transport any changes from each end to the other, but that’s database replication, a complicated thing to configure and use, never mind write, and your database format might not make it possible.

So possibly the easiest way is a bit of code to export updated prices from Access and upload it to the host, and an import routine on there, and the opposite way to move stuff from the host to the internal copy.

This article may help some.

My take is there are two main questions.

  • do you have a Windows host?
  • is the ODBC driver enabled?

Hi Mittineague

i dont have window hosting. i have linux web hosting on server.

i have already read that article
but that article also suggests to put or upload the access database file online.

i was searching for a way by which php can communicate with offline file which i dont have to upload online everytime i make changes to its data

vineet

Using your home computer as an online server has so many complications and potential pitfalls it’s essentially a no starter.

Likewise, having two versions of the same database both that can change at any given time in sync can be a huge problem.

Unless you are willing to maintain multiple servers with fall back in place and hire personnel to keep things running you will have problems.

This is why the typical approach is to have only a single live database (not counting the backup files) and work with that.

I’m guessing the online database would be the more active and that your computers database would have only infrequent changes after it is initially populated.

So it makes sense to me for you to do any CRUD (Create, Read, Update and Delete) online and avoid the need to do any complicated network or syncing operations.

IMHO if you are wanting to do this to save money by not getting an online database host you are grossly underrating the value of your time.

why would i think about saving money by not getting online database ??

i already have an ecommerce website running on the linux hosting server with mysql database.

i just wanted to sync that ecommerce website database with the desktop accounting database.

the accounting software company doesnt provide any online api to connect to their database.

so the accounting software database file remains just on the computer hard drive in all ways.

i wanted to get out of the hurdle of everytime uploading and downloading the database file.

but i think there is no other way except the import and export way

vineet

I guess you might be able to migrate an Access database to a MySQL database. But that is likely to be a lot more work and have more problems than having an Access database server.

Exactly why the typical approach is to work with the online database. Then you only need to download backup files.

The lowest hurdle is not doing networking and syncing, the lowest hurdle is getting the accounting software database online so you can work with it online.

Hi Mittineague

do you have any idea about desktop odbc software which is in the control panel

is that odbc software limited to just the windows applications only.

does it make any online communication with web server or not ??

vineet

Hmm, I just installed SQL Server Data Tools into my Visual Studio but I see something different.

It looks like “connect to database” is what you would want.

then enter the server info

hi Mittineague

i was not talking about sql server control panel

i was talking about windows xp control panel or windows 7 control panel

there is odbc software inside windows xp control panel > administrative tools


vineet

Well, I have Windows 10, although the UI looks different I did find the ODBC Data Source Administrator.

It may be under the File DSN tab.

But I have a feeling you will need to use the
SQL Server Client Network Utility
which I don’t have.

yes thats the tool i am talking about.

can it be used to communicate with php file on linux server online ??

vineet