How to connect to MS Access. PHP and ODBC
By Sergey Skudaev
![]()
![]()
![]()
![]()
![]()
![]()
In this tutorial I give you PHP code example connection to MS Access database. It is easy if you run Web Server on Widows PC. For Unix special driver is required. This tutorial teaches you how to connect to MS Access on Windows PC. Also, you will learn how to write SQL query to insert data to MS Access database or to select data from MS Access database.
Create MS Access database with credit_card table from. See my tutorial Data Model and Database project
create table credit_card(
cardid INT NOT NULL ,
name varchar(20),
type varchar(20),
expired date,
card_num INT,
credit float,
phone varchar(12),
address varchar(100),
city varchar(20),
state varchar(2),
zip varchar(10),
PRIMARY KEY(cardid)
);
Insert data in the table: copy and paste query in SQL View window.
Insert into credit_card (
cardid, name,type,expired,card_num,credit,phone,address,city,state,zip)
values
(1, 'CHASE','Visa','2006-01-01', 123456789, 20000.00 ,'123-123-1234',
'12 West 13th Street','New York','NY','12345-4321');
Insert into credit_card (
cardid, name,type,expired,card_num,credit,phone,address,city,state,zip)
values
(2, 'Bank One','MasterCard','2006-01-07', 987654321, 10000.00 ,'123-123-9876',
'16 West 17th Street','New York','NY','12345-4321');
Insert into credit_card (
cardid, name,type,expired,card_num,credit,phone,address,city,state,zip)
values
(1, 'Chase Platinum','MasterCard','2006-09-07', 999996789, 15000.00 ,'123-876-9876',
'19 West 19th Street','New York','NY','12999-4321');
See Data Model and Database project on this web site for details how to execute query in Access.
Create datasource name
Click Start, Settings, Control Panel...
In Windows 2000, XP, Vista ODBC located inside Administrative Tools folder. Double click ODBC Data Sources. ODBC Data Source Administrator window displays.
Select System DSN tab and click Add button. ODBC Microsoft Access Setup window displays. Type credit for data source name and click Select button. Select Database window displays. Find your database and click OK button. Click OK on Microsoft Access Setup window and OK on ODBC Data Source Administrator window.
PHP script:
<?php
$conn=odbc_connect("credit","" ,"");
print('<html>');
print('<head>')
print('<title>PHP and MS ACCESS</title>')
print('</head>');
print('<body>');
print('<table align="center" width="90%">');
print('<tr><th>CardID</th><th>CardName</th><th>Type</th>
<th>Expire</th><th>Number</th><th>Credit</th><th>Phone</th>
<th>Address</th><th>City</th><th>State</th><th>Zip</th></tr>');
if($conn)
{
$sql="select * from credit_card";
$row=odbc_exec($conn, $sql);
while(odbc_fetch_row($row))
{
$cardid=odbc_result($row,1);
$name=odbc_result($row,2);
$type=odbc_result($row,3);
$expired=odbc_result($row,4);
$card_num =odbc_result($row,5);
$credit =odbc_result($row,6);
$phone =odbc_result($row,7);
$address =odbc_result($row,8);
$city =odbc_result($row,9);
$state =odbc_result($row,10);
$zip =odbc_result($row,11);
print('<tr><td>'.$cardid.'</td><td>'. $name.'</td><td>'.$type.'</td>
<td>'.$expired.'</td><td>'.$card_num.'</td><td>'.$credit.'</td><td>'. $phone.'</td><td>'.$address.'</td><td>'.$city.'</td><td>'.$state.'</td>
<td>'.$zip.'</td></tr>');
}
print('</table>');
print('</body>');
print('</html>');
Good luck!
Please rate the tutorial
Learn SQL Programming By Examples [Kindle Edition]2.99
Learn PHP Programming by Examples [Kindle Edition] $2.99
Learn Visual Basic 6.0 [Kindle Edition] $1.99
How to Build Your Own Web Site from Scratch [Kindle Edition] $1.49
New-trip.com website source code
| Comments | |
|---|---|
- Home
- php_access
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- PHP Code Examples
- Learn PHP
- Modeless Popup
- Read Screen Resolution
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- GET mess
- Array of Check boxes
- PHP and MS Access
- User_Auth. Demo
- PHP App Example
- PHP + Oracle 1
- PHP + Oracle 2
- Image Collection
- Create Gallery
- Block Spam Post
- $_REQUEST,$_SESSION
- Headers Allredy Sent
- Virtual Server
- Apache:OS 10048
- Set Shopping Cart
- My Best CSS Layout
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Site map
- Registration
Web programming Tips