Connecting PHP With the ODBC

Preview:

DESCRIPTION

Database connectivity using ODBC in PHP

Citation preview

Connecting PHP with the ODBC :

(a) Creating a database

We are creating the database in MS Access 2003

(i) Start the Microsoft Access 2003

(ii) The microsoft access window will

get displayed on the screen

(iii) Click on the new button and select the blank database option from the menu displayed on the extreme right.

(iv) Click on the blank database option , it will display the save as dialog box to save the database. The extension name for the microsoft access database is .mdb(microsoft database)

Save the file in the web directory.

In our case , we will save the file data.mdb in the directory ,

mdb(Microsoft DataBase)

c:\wamp\www\phpextra\cms.mdb

(v) The database main window , will appear on the screen .

(vi) In the tables section, double click on the "Create table in Design View"

(vii) The table designer dialog box will appear on the screen,in this we have to specify the field structure .

(viii) Now, click on the cross button to save the table .

It will prompt us to save the file .

click on Yes button to save the file.

Specify the table name and click on the ok button

Then , it will prompt for the primary key ,

So, click on the no button , if you clicked on yes , it will add another

column to the table , which will be auto numbered.

(ix) Now, the main database dialog box ,

will list that table.

Now , double click on the table name to add records in it.

And click on the close button to save the records.

Creating the DSN

DSN stands for the Data Source Name.It is the name assigned to the database through which we will reference the database in the JSP code.

Steps for creating the DSN

(a) From the Start Menu , select the Control Pannel Option

(b) In the Control Pannel window, select the Administrative Tools Option .

(C) Double click on the Administrative Tools option , and form its window select the ODBC(Data sources) option .

(E) Double click on the ODBC (Data Sources) , the ODBC Data Source Administrator dialog boxwill appear on

the screen , click on the System DSN tab , and the click on the Add button to create a new DSN.

(F) The Create New Data Source dialog box will appear on the screen .

If you want to create a DSN which will be linked with the MS Access database , select the Driver do Microsoft Access (*.mdb) from the list .

If you want to create the DSN which will interact with the SQL Server database select the SQL Server.

Then click on the Finish button.

(G) The ODBC Microsoft Access Setup dialog box will appear on the screen in which you can specify the DSN name and the location of the database.

To specify the location click on the select button .

(H) Our created DSN will appear in the list of User DSN

PHP Database ODBC

ODBC is an Application Programming Interface (API) that allows you to connect to a data source (e.g. an MS Access database).

Create an ODBC Connection

With an ODBC connection, you can connect to any database, on any computer in your network, as long as an ODBC connection is available.

Here is how to create an ODBC connection to a MS Access Database:

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.

Q . WAP to list all records

<html><head><title>Using the ODBC</title></head><body bgcolor=cyan><center><table border=2><tr>

<th>Employee ID</th><th>Name</th><th>Salary</th>

</tr><?php

//connect to the database

$conn=odbc_connect('emsdb','','');

//define the sql query

$sql="SELECT * FROM employee";

//execute the query

$rs=odbc_exec($conn,$sql);

//fetch the row

while(odbc_fetch_row($rs) ){

//reterive the value of the record

$eid=odbc_result($rs,1); $ename=odbc_result($rs,2); $esal=odbc_result($rs,3);

echo "<tr><td>$eid</td>";echo "<td>$ename</td>";echo "<td>$esal</td></tr>";

}

echo "</table>";?>

</body></html>

Recommended