Reading a Database

Reading a DatabaseLearn how to display data from a Database.

Displaying data from a database is one of the most common uses of ASP.
Here's some sample code:

<%Dim ConnDim SQLTempSet Conn = Server.CreateObject("ADODB.Connection")Conn.connectionstring = 	"Provider=Microsoft.Jet.OLEDB.4.0;Data 	Source=" & Server.MapPath("yourdb.mdb")& ";" Conn.Open SQLTemp = "SELECT * FROM yourTable"set rstemp=Conn.execute(SQLTemp)Do While Not rstemp.EOF%><%= rstemp.Fields("pID").Value %>: <%= rstemp.Fields("pSubj").Value %>, <%= rstemp.Fields("pDate").Value %>, <%= rstemp.Fields("pText").Value %><br><%rstemp.MoveNextLoopConn.CloseSet Conn = Nothing%>

That was a simple select query.
Here it is broken down:The first two lines declare the variables Conn and SQLTemp.The next six lines open a connection to your database, and get all therows from the table. The following Do While Not loop lets u display thedata inside the row. The next four lines print out the value of eachcolumn in that row. The last bit moves to the next row, ends the loop,and closes the connection to the database.

If you decide you want the data ordered by date, you can use the following code in place of the original SQLTemp:
SQLTemp = "SELECT * FROM yourTable ORDER BY pDate"

Adding DESC or ASC to the end of the query as such:
SQLTemp = "SELECT * FROM yourTable ORDER BY pDate DESC"
will change the order the data is sorted in.

Nottoo hard, was it? The last bit we'll cover is how to limit the results,and here we'll limit it to showing only the first 30 results:
SQLTemp = "SELECT * FROM yourTable LIMIT 30"

Any combination of these queries can be mixed and match to achieve results you want.

NOTE:Had trouble connecting to the database? Click here.

Related Articles

  • Retrieving Data From a Database
    Before getting started, Ill assume you already know how to connect & select the database of your choose.
    Retrieving data from a database is a common task for web database applications, the most common use is to display the data in a web page.
  • Database Connection Strings
    Although our ASP database tutorials all use MS Access connection strings, there are many sorts of database software out there for use with ASP. This tutorial will show you how to connect with them...
  • Inserting Rows into a Database
    This tutorial will show you how to insert a row of data into a database using ASP and an MS Access database. You can do it on one ASP file, but well have two files: one processor page (proc.asp), and one form page (form.asp). Then, well make the MS Access database that corresponds to the ASP.
  • Users Online
    First Run this SQL In your MYSQL database..
  • Alternating Row Colors
    Learn how to alternate row colors from database output using ASP.
  • PHP Navigation and Connecting to a MySQL Database
    PHP Navigation and Connecting to a MySQL Database
  • Good Web Design - What is it?
    There is unlimited possibilities when it comes to designing a website. Flash, Database, content management, Client Login, etc. A question to ask yourself when deciding how to do your website is:...
  • Alternating Backgrounds
    Alternate the backgrounds of table rows displayed from a MySQL database.
  • URL Verification
    Do you have a big list of links in your database that you want to display but only want to show ones that are still in existance? Well theres a very easy method for doing so. All you need is the fsockopen() function. Here is an example:
  • Hit Counter
    Hit counters are used to track the amount of visitors there have been to a cetain page. I have looked through the internet and I have come up with 2 different ways to produce a hit counter.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.