<%
'================================================= ==============================
' User Settings Coded By: Dan Barnett
'================================================= ==============================
Const intDatabaseType = 1 '1=MySQL, 2=Access, 3=SQL Server
Const strServer = "localhost" 'Server URL or localhost
Const strDatabase = "DBname" 'Database Name
Const strUserID = "username" 'Database User ID
Const strPassword = "password" 'Database Password
Const strDefaultTable = "users" 'Default table for database explorer
Const strDefaultColumn = "username" 'Default column for database explorer
'-------------------------------------------------------------------------------
strScriptName = Request.ServerVariables("Script_Name")
If Session("loggedin") = True And Request.QueryString() <> "logout" Then
Dim strValue, strSQL, strLeft, strRight, strScriptName, Conn, RS
If intDatabaseType = 1 Then
strLeft = "`"
strRight = "`"
Else
strLeft = "["
strRight = "]"
End If
Set Conn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.Recordset")
Conn.Open = SQLConnect(intDatabaseType, strServer, strDatabase, strUserID, strPassword)
If Request("query") = "" And Request.QueryString("sortby") = "" And Request.QueryString("order") = "" Then
strSQL = "SELECT * FROM " & strLeft & strDefaultTable & strRight & " ORDER BY " & strLeft & strDefaultColumn & strRight & " ASC;"
Session("columns") = " * "
Session("where") = " "
Session("table") = strDefaultTable
ElseIf Request.Form("table") <> "" And Request.Form("table") <> "-1" Then
strSQL = "SELECT * FROM " & strLeft & Request.Form("table") & strRight & ";"
Session("columns") = " * "
Session("where") = " "
Session("table") = Request.Form("table")
Else
If Request.QueryString("sortby") <> "" Or Request.QueryString("order") <> "" Then
Dim strSortBy, strOrder
strSortBy = Request.QueryString("sortby")
strOrder = UCase(Request.QueryString("order"))
If strSortBy = "" Then strSortBy = strDefaultColumn
If strOrder <> "ASC" Then
If strOrder <> "DESC" Then strOrder = "ASC"
End If
strSQL = "SELECT" & Session("columns") & "FROM " & strLeft & Session("table") & strRight & Session("where") & "ORDER BY " & strLeft & strSortBy & strRight & " " & strOrder & ";"
Else
strSQL = Request("query")
Dim intColumnPos, intTablePos, strTable, intWherePos
intColumnPos = InStr(strSQL, "FROM") - 7
Session("columns") = Mid(strSQL, 7, intColumnPos)
If InStr(strSQL, " WHERE ") > 0 Then
intTablePos = InStr(strSQL, "FROM") + 5
strTable = Mid(strSQL, intTablePos)
intTablePos = InStr(strTable, " ")
Session("table") = StripDB(Left(strTable, intTablePos), 0)
Dim intTablePos2, intOrderByPos
intTablePos2 = InStr(strSQL, strLeft & Session("table") & strRight) + 7
strSQL = Mid(strSQL, intTablePos2)
intOrderByPos = InStr(strSQL, "ORDER BY") - 1
If InStr(strSQL, "ORDER BY") > 0 Then
Session("where") = Mid(strSQL, 1, intOrderByPos)
Else
intWherePos = InStrRev(StripDB(strSQL, 1), " ") + 1
Session("where") = Mid(StripDB(strSQL, 1), intWherePos)
End If
Else
If InStr(strSQL, "ORDER BY") > 0 Then
intTablePos = InStr(strSQL, "FROM") + 5
strTable = Mid(strSQL, intTablePos)
intTablePos = InStr(strTable, " ")
Session("table") = StripDB(Left(strTable, intTablePos), 0)
Else
intTablePos = InStrRev(StripDB(strSQL, 1), " ") + 1
Session("table") = Mid(StripDB(strSQL, 1), intTablePos)
End If
Session("where") = " "
End If
strSQL = Request("query")
End If
End If
%><!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" xml:lang="en">
<head>
<title>Database Explorer</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us">
<style type="text/css">
a:link, a:visited, a:active { color: blue; }
a:hover { color: red; }
div { text-align: center; margin-bottom: 10px; }
table { border-collapse: collapse; border: 2px solid #000; }
td, th { border : 1px solid #000; }
th { background: #b0c4de; }
input { width: 50%; }
h2 { margin-top: 0; margin-bottom: 0; text-align: center; }
#logout { position: absolute; left: 3px; top: 3px; width: 10px; }
.italictext { font-style: italic; }
</style>
</head>
<body>
<div id="logout">[<a href="<%=strScriptName%>?logout" title="Logout">Logout</a>]</div>
<h2>Exploring Database <%=strDatabase%></h2>
<div>
<form method="post" action="<%=strScriptName%>">
Query: <input type="text" name="query" value="<%=strSQL%>"> <button type="submit">Submit</button><br />
- OR -<br />
SELECT * FROM <select name="table">
<option class="italictext" selected="selected" value="-1">Select One</option>
<%
RS.Open Conn.OpenSchema(20)
While Not RS.EOF
Response.Write(Tab(5) & "<option value=""" & RS("TABLE_NAME") & """>" & RS("TABLE_NAME") & "</option>" & vbCrLf)
RS.MoveNext
Wend
RS.Close
%>
</select> <button type="submit">Submit</button>
</form>
</div>
<hr />
<table>
<tr>
<%
RS.Open strSQL, Conn
For Each x In RS.Fields
Dim strOrder2, strName
strName = x.Name
strOrder2 = "ASC"
If Request.QueryString("sortby") = strName And Request("order") = "ASC" Then strOrder2 = "DESC"
Response.Write(Tab(4) & "<th><a href=""" & strScriptName & "?sortby=" & strName & "&order=" & strOrder2 & """ title=""Sort by column " & strName & """>" & strName & "</a></th>" & vbCrLf)
Next
Response.Write(Tab(3) & "</tr>" & vbCrLf)
While Not RS.EOF
Response.Write(Tab(3) & "<tr>" & vbCrLf)
For Each x In RS.Fields
strValue = x.Value
If strValue = "" Or IsNull(strValue) Then
If strValue = "" Then
strValue = "<span class=""italictext"">Empty</span>"
Else
strValue = "<span class=""italictext"">Null</span>"
End If
Else
strValue = StripHTML(strValue)
End If
Response.Write(Tab(4) & "<td>")
If CInt(Len(strValue)) > 100 Then
Response.Write(Left(strValue, 97) & "...")
Else
Response.Write(strValue)
End If
Response.Write("</td>" & vbCrLf)
Next
Response.Write(Tab(3) & "</tr>" & vbCrLf)
RS.MoveNext
Wend
RS.close
%>
</table>
</body>
</html>
<%
Set RS = Nothing
Set Conn = Nothing