Computer Programming web Web programming Tips



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 Windows 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 a 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, mytype, 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 a query in Access.

Create datasource name

Click Start, Settings, Control Panel...

In Windows, ODBC located inside the Administrative Tools. Double click ODBC Data Sources. ODBC Data Source Administrator window displays.

ODBC Data Source

Select the System DSN tab and click Add button. ODBC Microsoft Access Setup window displays. Type 'credit' for the data source name and click the Select button. The Select Database window is displayed. 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!

My eBooks on Amazon.com

US    UK    BR    CA
US    UK    BR    CA
US   UK   BR   CA