|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Searching a database
I want to search my database using a person's name. My names are separated into 'First' and 'Last' name fields.
The SQL I'm using so far works if you search only by the first name, or only by the surname, but if you enter both names, it comes back with no results. How do you do it so that it will work in all cases? Here's my code: Sql = "SELECT * FROM kbs, authors WHERE authors.authorID = kbs.author" If Request.Form("TypeSearch") = "author" Then Sql = Sql & " AND (authors.authorLastName LIKE '%" & Request.Form("kbsSearch") & "%' OR authors.authorFirstName LIKE '%" & Request.Form("kbsSearch") & "%')" End If set rs = Server.CreateObject("ADODB.Recordset") set rs=conn.execute(Sql) |
|
#2
|
|||
|
|||
|
You need to split the kbsSearch into a first and lastname variable.
Then use these variables in your sql. The next question, what if only one name is entered? When use the vb split command, it creates an array of the splitted "words", if the number of elements in this array is 1, then only one word was entered, thus set both the firstname and lastname variables to this word... Should work in theory ![]()
__________________
How can I soar like an eagle when I'm flying with turkey's? |
|
#3
|
|||
|
|||
|
I tried this:
If Request.Form("TypeSearch") = "author" Then nameArray = split(TypeSearch, " ") firstName = nameArray(0) lastName = nameArray(1) if count(nameArray) = 1 then Sql = Sql & " AND (authors.authorLastName LIKE '%" & Request.Form("kbsSearch") & "%' OR authors.authorFirstName LIKE '%" & Request.Form("kbsSearch") & "%')" else Sql = Sql & " AND (authors.authorLastName LIKE '%" & lastName & "%' OR authors.authorFirstName LIKE '%" & firstName & "%')" End If End If but I got an Subscript out of range: '[number: 0]' error message. I'm guessing that my 'nameArray =' line isn't right. Should it be set to split 'TypeSearch' or 'author' and am I right to be splitting on "(space)" or do I have to enter an integer that represents that character? |
|
#4
|
|||
|
|||
|
try:
nameArray = split(Request.Form("kbsSearch")," ") ![]() |
|
#5
|
|||
|
|||
|
Now my error message has changed from [number: 0] to [number: 1].
Also, I wasn't sure about this line: if count(nameArray) = 1 then being the right way to tell if my array has only one value or not and when I try a two word search now I get Type mismatch: 'count' . |
|
#6
|
|||
|
|||
|
Better yet....
Code:
If Request.Form("TypeSearch") = "author" Then
nameArray = split(Request.Form("kbsSearch"), " ")
if IsArray(nameArray) and UBound(nameArray) > 1 Then
firstName = nameArray(0)
lastName = nameArray(1)
Sql = Sql & " AND (authors.authorLastName LIKE '%" & lastName & "%' OR authors.authorFirstName LIKE '%" & firstName & "%')"
else
Sql = Sql & " AND (authors.authorLastName LIKE '%" & Request.Form("kbsSearch") & "%' OR authors.authorFirstName LIKE '%" & Request.Form("kbsSearch") & "%')"
End If
End If
I forgot about asp built-in type checking. See how you go. |
|
#7
|
|||
|
|||
|
That returned a list of everything in the database (good thing it's not too big!)
![]() |
|
#8
|
|||
|
|||
|
Ok, so the first bit works the
![]() Next, I would start some debugging, such as what values are being stored in first and lastname i.e. Response.Write lastName & "-" & firstName If either of these come up blank, then you'll get all records back. Also it might be worth changing the sql to look for both first and last as follows Code:
Sql = Sql & " AND (authors.authorLastName LIKE '%" & lastName & "%' AND authors.authorFirstName LIKE '%" & firstName & "%')" We'll get there ![]() |
|
#9
|
|||
|
|||
|
Quote:
Yeah, it looks like my fields are coming up blank. ![]() |
|
#10
|
|||
|
|||
|
What is the value of "kbsSearch" that is coming through?
Response.Write Request.Form("kbsSearch") |
|
#11
|
|||
|
|||
|
I've been fiddling around with it for a while, but I'm not getting anything through when I <% Response.Write Request.Form("kbsSearch") %>.
But how can that be - I know it's reading values from the form when I do a search on title or even on author when it's only looking for one word? I also did a quick test on whether the set up of my array was working properly: Code:
<%
dim terms
terms = ("My Name")
dim nameArray
nameArray = split(terms, " ")
'if IsArray(nameArray) and UBound(nameArray) > 1 Then
firstName = nameArray(0)
lastName = nameArray(1)
'End If
response.write(firstName) & "<br>"
response.write(LastName)
%>
and I got results back when I commented out the if IsArray line, but I guess my problem's before it even gets that far, because it's not reading anything into the array? |
|
#12
|
|||
|
|||
|
Try using the split command without the delimiter
i.e. split(terms) I've just had a look at DevGuru - Split and found that split, by default, works on spaces, unless directed otherwise. See how you go... ![]() |
|
#13
|
|||
|
|||
|
I'm a bit closer, but still not quite there.
Code:
Dim searchTerm
searchTerm = Request.Form("kbsSearch")
Dim nameArray
nameArray = split(searchTerm)
If Request.Form("TypeSearch") = "author" Then
if IsArray(nameArray) and UBound(nameArray) > 1 Then
'response.write(nameArray(0))
Sql = Sql & " AND (authors.authorLastName LIKE '%" & nameArray(1) & "%' OR authors.authorFirstName LIKE '%" & nameArray(0) & "%')"
else
Sql = Sql & " AND (authors.authorLastName LIKE '%" & Request.Form("kbsSearch") & "%' OR authors.authorFirstName LIKE '%" & Request.Form("kbsSearch") & "%')"
End If
End If
Now, this works if I enter just one search term (either first or last name), but brings up the "we did not find any records" message if I enter a two word search. I tried commenting out the query and inserting the response.write(nameArray(0)) and got the same thing. But if I put the response.write ahead of the If Request.Form("TypeSearch") it prints out what was entered. If I comment out the if IsArray(nameArray) and UBound(nameArray) > 1 Then line the search works for two words but brings up an error message like I originally had (see first post) for just a one word search. |
|
#14
|
|||
|
|||
|
Try this:
Code:
Dim searchTerm
searchTerm = Request.Form("kbsSearch")
Dim nameArray
nameArray = split(searchTerm)
If Request.Form("TypeSearch") = "author" Then
'if nameArray has two elements (upper bound =1) then we have
'a first and last name, if we don't have two elements, something was wrong
'in the split (ie we got more names)
if IsArray(nameArray) and UBound(nameArray) = 1 Then
'response.write(nameArray(0))
Sql = Sql & " AND (authors.authorLastName LIKE '%" & nameArray(1) & "%' OR authors.authorFirstName LIKE '%" & nameArray(0) & "%')"
else
Sql = Sql & " AND (authors.authorLastName LIKE '%" & Request.Form("kbsSearch") & "%' OR authors.authorFirstName LIKE '%" & Request.Form("kbsSearch") & "%')"
End If
End If
|
|
#15
|
|||
|
|||
|
Yep, changing the 0 to 1 did it!!!
Thanks for all your help mate! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > Searching a database |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|