Using MySQL with ASP - Connecting with ASP
(Page 2 of 4 )
After all that hard work setting up the connections, you probably want something a little light to do. Well, that's just what it is to connect to a MySQL database from ASP. It is very simple. Below is the code that does the job:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="Templates/guestbook.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
<link href="user.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="1">
<tr>
<td colspan="2" class="welcomeheader">Welcome to Davids Website </td>
</tr>
<tr>
<td colspan="2"><!-- InstanceBeginEditable name="EditRegion3" -->
<table width="100%" border="0">
<%
Set conx = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
conx.Open "DSN=mysqlasp" ' Data source name
strSQL = "SELECT * FROM guests"
' Execute SQL statement
Set rs = conx.Execute(strSQL)
Do while not rs.eof
%>
<tr>
<td width="13%" bgcolor="#999999"><strong>Name:</strong></td>
<td width="87%"><% Response.Write rs("name") %></td>
</tr>
<tr>
<td bgcolor="#999999"><strong>Comment:</strong></td>
<td><% Response.Write rs("comment") %></td>
</tr>
<tr>
<td bgcolor="#999999"><strong>Date:</strong></td>
<td><% Response.Write rs("dateAdd") %></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<%
rs.MoveNext
loop
conx.close ' Close database connection
Set conx = nothing 'obj variable released
%>
</table>
<!-- InstanceEndEditable --></td>
</tr>
<tr>
<td colspan="2" class="copy">©2008</td>
</tr>
</table>
</body>
<!-- InstanceEnd --></html>
Next: Script Explained >>
More ASP Articles
More By David Web