View Full Version : Database Search
The Wizzard
12-21-2002, 08:53 PM
Im looking for a script for searching my database...
whammy
12-22-2002, 03:34 AM
A script to search your database? For what?
Usually you'd write one using SQL... can you explain in a LOT more detail?
Mhtml
12-22-2002, 03:38 AM
Are you going to be searching dynamic content? IE is the content going to be displayed on a page? Are there multiple tables which require different viewing pages?
OR are you just needing to list records?
whammy
12-22-2002, 03:53 AM
This should give you a start... there's probably a better way to just get the field names using SQL - but this only selects the top record to get each field name in your database, and writes them all to a select dropdown.
You can see how you could then make other dropdowns, with user-entered text, and then display whether or not any records were found in the selected field, etc. (just like any other form):
<%
Dim Conn, rs
Call OpenConnection()
Call GetFields()
Call DisplayFields()
Call CloseConnection()
Sub OpenConnection()
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Mid(Server.MapPath("\"), 1, InStrRev(Server.MapPath("\"),"\")-1) & "\database\scripts.mdb;" & _
"Persist Security Info=False;"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open sConnString
End Sub
Sub GetFields()
Set rs = Server.CreateObject("ADODB.RecordSet")
GetFieldsQuery = "SELECT TOP 1 * FROM scripts"
Set rs = Conn.Execute(GetFieldsQuery)
End Sub
Sub DisplayFields()
%>
<html>
<head>
<title>Example</title>
</head>
<body>
<div>
<form id="searchdatabase" action="javascript://">
<select name="fields">
<% Call ListEachField() %>
</select>
</form>
</div>
</body>
</html>
<%
End Sub
Sub CloseConnection()
rs.Close
Set rs = Nothing
Conn.close
Set Conn = Nothing
End Sub
Sub ListEachField()
For each Fld in rs.Fields
Response.Write(" <option value=""" & Fld.Name & """>" & Fld.Name & "</option>" & vbCrLf)
Next
End Sub
%>
Now that you have that, you can make a couple of other fields in your search form, like a dropdown that is either "equals" or "contains" for instance, and another text field to search by. Good luck!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.