CSCI 4900/6900 -- Web programming

Maymester 2001

Database access from ASP pages

Although we won't be programming ASP pages, we will be running database-access server scripts using PHP. The same general principles apply, only the syntax and object models differ somewhat.

Database components and languages

The Microsoft Data Access Model

The MS data model involves these key technologies and standards: There are also non-Microsoft products which allow you to access ODBC data sources by providing an ODBC driver. In general, the data source is assumed to be on the same computer as the Web server.

Using the ADO data model

The key objects in the ADOP data model are:

Establishing a connection

Here is the server-side code to establish a database connection:
dim oConn
set oConn = server.createObject("ADOBD.connection")
oConn.open "dsnStudentRecords"
This creates a connection object and binds it to the DSN "dsnStudentRecords". Halata's code doesn't check for success, which I recommend doing.

In Halata you will see that the connection.open() method can be called with any one of several different parameter sets. Here is the executive overview:

Retrieving records

You can create a recordset which contains all of the records in a table:
dim oRS
set oRS = server.createObject("ADODB.recordset")
oRS.open("products")
This will retrieve all the records in the table "products. Alternatively you can use an SQL SELECT statement to retrieve only a selected subset of records:
dim oRS
set oRS = server.createObject("ADODB.recordset")
oRS.open("SELECT * FROM products WHERE productType='software'")
This will retrieve only the records whose type field has the value "software".

Once you have retrieved the records into a recordset you can then traverse the recordset to access the individual records:

response.write("<table><th>Product<th>Type<th>Price")
do while not orS.EOF
response.write("<tr>")
response.write("<td>" & oRS.productName)
response.write("<td>" & oRS.productType)
response.write("<td>" & CurrencyFormat(orS.price))
orS.movenext
loop
response.write("</table>")
This will create a table with one row for each product record in the recordset oRS.

Closing the recordset

It is recommended to close the recordset and recycle its memory after use:
orS.close
set orS = nothing
I suspect that recordset resources are automatically garbage-collected when the script ends. But it is always good housekeeping to close anything you open!

Learning PHP

We'll use the online references, headquartered at www.php.net:

 Very short tutorial

 PHP info script

 PHP manual