Connecting PHP With the ODBC

23
Connecting PHP with the ODBC : (a) Creating a database We are creating the database in MS Access 2003 (i) Start the Microsoft Access 2003

description

Database connectivity using ODBC in PHP

Transcript of Connecting PHP With the ODBC

Page 1: Connecting PHP With the ODBC

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

Page 2: Connecting PHP With the ODBC

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.

Page 3: Connecting PHP With the ODBC

(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)

Page 4: Connecting PHP With the ODBC

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 .

Page 5: Connecting PHP With the ODBC

(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 .

Page 6: Connecting PHP With the ODBC

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

It will prompt us to save the file .

Page 7: Connecting PHP With the ODBC

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 ,

Page 8: Connecting PHP With the ODBC

will list that table.

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

Page 9: Connecting PHP With the ODBC

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.

Page 10: Connecting PHP With the ODBC

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 .

Page 11: Connecting PHP With the ODBC

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

Page 12: Connecting PHP With the ODBC

(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.

Page 13: Connecting PHP With the ODBC

(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.

Page 14: Connecting PHP With the ODBC

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.

Page 15: Connecting PHP With the ODBC

To specify the location click on the select button .

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

PHP Database ODBC

Page 16: Connecting PHP With the 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.

Page 17: Connecting PHP With the ODBC

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

Page 18: Connecting PHP With the ODBC

$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>