I have a small database of about 30 names that are all unique. The variable $member equals "Ann" but will change depending on the person to search for. I need to be able to return the exact match of the person's first name. I have tried many combinations of the Where clause but I cannot get it to return "Ann" instead of "Anna" since Anna is in the table before Ann.

This returns "Anna" instead of "Ann":
$query = "SELECT * FROM board_members where FirstName like '%$member%'";

These returns nothing:
$query = "SELECT * FROM board_members where FirstName = '$member'";
$query = "SELECT * FROM board_members where FirstName = '%$member%'";

Can someone help me with this?

Recommended Answers

All 8 Replies

Your 2nd query is correct. If it does not return data, then the data are wrong. Show a sample.
And how is "Anna" before "Ann" in your table? You don't have any ORDER BY clause in your query, so the word "before" makes no sense here.

If I read this correctly, there is nothing wrong with your first SELECT statement. It is returning Anna because that is (in your own words) the first record returned. Ann is probably record 2. If you want to return the records sorted, add ORDER BY $member ...

$query = "SELECT * FROM board_members where FirstName like '%$member%' ORDER BY $member";

This still gives me Anna instead of Ann.

Table of board_members

FirstName  |    LastName    |   Amount
======================================
Alona      |    Burnett     |   1050
Anna       |    Hartman     |   1200
Anne       |    Ferguson    |   555
Bobbie     |    McLaughlin  |   975

This gives me Anna instead of Ann.

$query = "SELECT * FROM board_members where FirstName like '%$member%' ORDER BY FirstName";

Using the following code gives me "Unknown column 'ann' in 'order clause'" error.

$query = "SELECT * FROM board_members where FirstName like '%$member%' ORDER BY $member";

From when I programmed in Visual Basic, you could use the equals sign for an exact match. But if I use the equals sign here instead of Like, I get nothing.

So where is "Ann" in your table? With those data an exact match query for "Ann" must return an empty set.
"Order by $member" is nonsense. You order by columns, not by fixed values.

Please skip this post and see the next one. I clicked wrong and cannot delete this one.
Thanks

I just realized that also. But the query needs to return nothing (which I will put a condition in for that) instead of returning "Anne" or "Anna" if the search is for "Ann". Again the search needs to be for an exact match and not a partial.

So test for an exact match, like in your 2nd query:

SELECT * FROM board_members where FirstName = '$member'"

Thanks smantscheff! That is working like a charm.

I made an Anna, Anne, and Ann in this order and it finds Ann like it should.
Partial searches on other names (Deb for Bebby, Jen for Jennifer) turns up empty which is correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.