ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Microsoft Access Database Connectivity (DSN-Less)
Submitter: Eric Guanlao (other recipes)
Last Updated: 2006/08/29
Version no: 1.3
Category: Platform Interfaces, Databases

 

5 stars 4 vote(s)


Approved

Description:

An example of connecting to a Microsoft Access database, executing an SQL query, and displaying the results in HTML using PHP and COM (Windows only).

NOTE: This works with PHP 4.x. I have been unsuccessful with getting this to work with PHP 5.1.6 and Apache 2.0.59 on Windowx XP Professional.

Source: Text Source

<?php

// Two versions of Microsoft Office.  Choose one.
//$db = 'C:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb';
$db = 'C:\\Program Files\\Microsoft Office\\Office10\\Samples\\Northwind.mdb';

$conn = new COM('ADODB.Connection') or exit('Cannot start ADO.');

// Two ways to connect. Choose one.
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');
//$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db") or exit('Cannot open with driver.');

$sql = 'SELECT   ProductName, QuantityPerUnit, UnitPrice
	FROM     Products
	ORDER BY ProductName';
$rs = $conn->Execute($sql);

?>

<table>
<tr>
	<th>Product Name</th>
	<th>Quantity Per Unit</th>
	<th>Unit Price</th>
</tr>
<?php while (!$rs->EOF) { ?>
	<tr>
		<td><?php echo $rs->Fields['ProductName']->Value ?></td>
		<td><?php echo $rs->Fields['QuantityPerUnit']->Value ?></td>
		<td><?php echo $rs->Fields['UnitPrice']->Value ?></td>
	</tr>
	<?php $rs->MoveNext() ?>
<?php } ?>
</table>

<?php

$rs->Close();
$conn->Close();

$rs = null;
$conn = null;

?>

Discussion:



Add comment

Number of comments: 28

PWS and PHP, Pier Luigi, 2003/03/26
I tried the Connection string by Gualano and Hendricks to connect PHP files to Microsoft Access Data Base with Windows 2000 and Internet Information server. They were perfect. BUT with Win98 and Personal Web server they didn't work!!! Any suggestion ?? Luigi Gaudio
Add comment

Re: PWS and PHP -- > Missing DCOM / MDAC, Bjørn Atle Vorland, 2003/10/28
You might be missing the latest DCOM http://www.microsoft.com/com/dcom/dcom98/download.asp or MDAC http://download.microsoft.com/download/3/b/f/3bf74b01-16ba-472d-9a8c-42b2b4fa0d76/mdac_typ.exe ,hope this helps. Regards Bjørn Atle
Add comment

noob question regarding this snippet, 2003/12/04
Will this code *not* work on unix-based servers? If not, how can I make a connection etc. to a .mdb-file on a unix-system? regards, Olof
Add comment

Another noob, Nicholas Gledhill, 2006/01/24
I've been trying to work out how to do this as well. I've found a lot of coverage of how to access Access without a DSN using COM but how do you do it from a UNIX platform. I'm trying to implement a new intranet, on Linux / Apache, but I need to be able to talk to some legacy Access .mdb files from my PHP web pages. Can anyone help?
Add comment

Another noob question, Nicholas Gledhill, 2006/01/24
I've been trying to work out how to do this as well. I've found a lot of coverage of how to access Access without a DSN using COM but how do you do it from a UNIX platform. I'm trying to implement a new intranet, on Linux / Apache, but I need to be able to talk to some legacy Access .mdb files from my PHP web pages. Can anyone help? Cheers, Nick Gledhill.
Add comment

An embarrassed noob (with an answer), Nicholas Gledhill, 2006/01/24
Having just embarrassed myself by posting the same message twice - I have now found this answer to the question - which I thought might help other people: Go to: http://aspn.activestate.com/ASPN/Mail/Message/370705 for details of a good way to solve the MS Access from Linux / Unix problem. Cheers, Nick Gledhill
Add comment

Connection Error, Gregory Carlisle, 2003/12/24
Hey, I'm usin WinXP, and when I tested that script it said "Parse error: parse error, unexpected T_VARIABLE in c:\web site\test.php on line 5" can anyone help? I checked the code and it was exactly as it was shown on the site, any help?
Add comment

Missing escape \? Or missing end ;, Rob MacDonald, 2004/04/19
Have you escaped all \'s with the \ and have you ended the line with a ;?
Add comment

$db = 'C:\\Inetpub\\wwwroot\\6\\a.mdb';, Rafi Abdul, 2005/04/15
can u find any problem in this code ... I am getting the same error as well .. Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';'
Add comment

Error Found in Code, Gregory Carlisle, 2003/12/24
Hey, I found an error in the code. At line 29 there should not be the () in MoveNext.
Add comment

New record, Geoffrey Hyson, 2004/03/29
Can a new record be inserted into a table using this method of connection?
Add comment

Add Record, Mogens Melander, 2004/04/26
Shure, $sql = "INSERT INTO TABLE(field1, field2,....) VALUES('$field1, $field2,....)";
Add comment

Mumthaz Sidik, 2005/04/07
the code is same as Eric Guanlao (other recipes) titled Microsoft Access Database Connectivity (DSN-Less) posted on 2003/02/11 --------- In the middle of the code from

Fields['ProductName']->Value
Fields['QuantityPerUnit']->Value
Fields['UnitPrice']->Value

rs.Update //here the code should be added to update before rs.MoveNext

Close();
$conn->Close();
?> OR
The second method
--On the Query---
-------- eg: $sql = 'insert into (ProductName,QuantityPerUnit,UnitPrice) values('Coke','100','10')'from products ;
$rs = $conn->Execute($sql);

Add comment

Inserting OR Updating Records in MSACCESS using SQL Query, Mumthaz Sidik, 2005/04/07
the code is same as Eric Guanlao (other recipes) titled Microsoft Access Database Connectivity (DSN-Less) posted on 2003/02/11 --------- In the middle of the code from

Fields['ProductName']->Value
Fields['QuantityPerUnit']->Value
Fields['UnitPrice']->Value

rs.Update //here the code should be added to update before rs.MoveNext

Close();
$conn->Close();
?> OR
The second method
--On the Query---
-------- eg: $sql = 'insert into (ProductName,QuantityPerUnit,UnitPrice) values('Coke','100','10')'from products ;
$rs = $conn->Execute($sql);

Add comment

PHP, Access, J S, 2005/05/01
I've tried running the code in the article and I keep getting a PHP Script Interpreter error. When I replace the "$conn->Open" statements with "$conn = odbc_connect()" statements, I don't get the Interpreter error, but I do get a PHP error: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause., SQL state 37000 in SQLExecDirect in . I'm sure my SQL statement is correct. What else may be wrong?
Add comment

r yjr, Vu Ton That, 2005/08/31
rt urt
Add comment

Questions about ->, Terry Kosowick, 2005/05/18
I'm just learning PHP. What does the -> mean? Or where can I find out about it? Thanks.
Add comment

Didn't work, Flávio Maia, 2005/12/21
I tried to use the code "as is", with no success on either method of connection. Then i tried to use an existing .mdb file of mine, but it didn't work either. I use Apache 1.3.34, PHP 5.1.1, Win XP. The .mdb file was created using Access XP.
Add comment

This works for me, Hasan Not specified, 2005/12/26
One of my setups is XP+IIS+PHP4.4.1 and I use the following for a DSN-less connection to Access:

 Comment - find the absolute path for scripting
 if(!isset($_SERVER["DOCUMENT_ROOT"])){
  $_SERVER["DOCUMENT_ROOT"]=str_replace('\\','/',getcwd());
 }

 Comment - connect to db One of my setups is XP+IIS+PHP4.4.1 and I use the following for a DSN-less connection to Access:

 Comment - find the absolute path for scripting
 if(!isset($_SERVER["DOCUMENT_ROOT"])){
  $_SERVER["DOCUMENT_ROOT"]=str_replace('\\','/',getcwd());
 }

 Comment - connect to db 

Add comment

My apologies..., Hasan Not specified, 2005/12/26
That didn't print correctly. But I just whipped up an HTML page where the complete code from my previous post can be viewed at http://www.marxmedia.net/tuts/php/dsnless/dsnless.htm

Enjoy!

Hasan
Add comment

php and Microsoft Access, Terry Gilliver, 2006/01/29
Hi, I am new to php and tried the php cookbook example, modified to my own needs. I get a 'Cannot open with JET' message or 'Cannot open with Driver' depending on which $conn->open statement I choose. Below is a sample of my code. $db = 'c:\\inetpub\\wwwroot\\experimental\\SunflyDatabase.mdb'; $conn = new COM('ADODB.Connection') or exit('Cannot start ADO.'); $conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');
Add comment

php and Microsoft Access, Terry Gilliver, 2006/01/29
Hi, I am new to php and tried the php cookbook example, modified to my own needs. I get a 'Cannot open with JET' message or 'Cannot open with Driver' depending on which $conn->open statement I choose. Below is a sample of my code. $db = 'c:\\inetpub\\wwwroot\\experimental\\SunflyDatabase.mdb'; $conn = new COM('ADODB.Connection') or exit('Cannot start ADO.'); $conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');
Add comment

php and Microsoft Access, Claudio Xerez, 2006/02/03
Hi! i was trying to implement htis code and after some tries i've made it work commenting it like below: $conn = new COM('ADODB.Connection');// or exit('Cannot start ADO.');
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");// or exit('Cannot open with driver.');
Add comment

can't open database, him seyha, 2006/09/25
'Cannot open with JET' message or 'Cannot open with Driver' depending on which $conn->open statement I choose. Below is a sample of my code. $db = 'c:\\inetpub\\wwwroot\\experimental\\SunflyDatabase.mdb'; $conn = new COM('ADODB.Connection') or exit('Cannot start ADO.'); $conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');
Add comment

PHP+Apache+MS ACCESS, Ivan Akcheurov, 2006/03/28
Please, can somebody tell me why this script doesn't work for PHP under Apache. I've tried this approach under IIS using ASP, works perfectly, but under Apache it says "Cannot open with jet". Please, help me to connect to DB from PHP under Apache without DSN.
Add comment

COM???, Iain Adams, 2006/04/20
I am new to this, but was wondering what is COM and is there anywhere I can find documentation on it.
Add comment

Problem with the code, Neko Chan, 2006/04/24
Hi everyone!

I'm trying to use this code but i have problems with the sign "->". Everytime it appears, php doesn't work.


If code is:


$db = 'C:\\Program Files\\Microsoft Office\\Office11\\SAMPLES\\Northwind.mdb';

$conn = new COM('ADODB.Connection') or exit('Cannot start ADO.');

// Two ways to connect. Choose one.
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');

//$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db") or exit('Cannot open with driver.');


...


When i open the page, the only thing it shows is:


Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.'); //$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db") or exit('Cannot open with driver.'); $sql = 'SELECT ProductName, QuantityPerUnit, UnitPrice FROM Products ORDER BY ProductName'; $rs = $conn->Execute($sql); ?> EOF) { ?> MoveNext() ?>

 Product Name  	Quantity Per Unit  	Unit Price 

Fields['ProductName']->Value ?> Fields['QuantityPerUnit']->Value ?> 	Fields['UnitPrice']->Value ?> Close(); $conn->Close(); $rs = null; $conn = null; ?>



So, what is the problem????

Thanks a lot

Add comment

Error When trying this code, Neil Swartz, 2006/07/24
I copied this code into Dreamweaver and executed it. Depending on the connection string I used I am getting the following errors: Cannot open with Jet Cannot open with driver I have MS Office 2003 installed and am using an Apache2 Server with PHP installed. Can anyone help???
Add comment



Highest rated recipes:

1. DB_eSession PHP class ...

2. main - python-like if ...

3. iPHP: Semi-interactive ...

4. tinySendMail

5. Microsoft Access ...

6. Single Linked List

7. PHP MySQL Search Class

8. Pass Javascript arrays ...

9. Microsoft Access ...

10. Password generation




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.