Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with DB search

Status
Not open for further replies.

dexthageek

IS-IT--Management
Mar 23, 2002
59
US
I am working a search tool for users to find members based on First Character of lastname.

How can I have mysql or PHP only look at the first character of the string?
 
Sorry, I will elaborate.

Ok, I have a list of names in a database, with 2 columns
'firstname' and 'lastname'

I am passing a single character from the alphabet (A B C) and I want to search for all values in column 'lastname' that begin with the variable I mentioned above.

Example:
Code:
//I want to display all lastnames that begin with 'M'
$query = "M"; 


$data = $db->query("SELECT * FROM {$tbl_prefix}users 
                    WHERE lastname = '$query'");

I hope you understand what I am trying to do now
 
$first_letter = "M"

$data = $db->query("SELECT * FROM {$tbl_prefix}users
WHERE lastname LIKE '$first_letter%'");

This will basically look for anything that begins with $first_letter and is followed by anything (% means anything)
 
ok awesome, I didnt even think that you could use % at just the end, I have used it at both the beginning and the end of a variable for the LIKE clause.

That worked perfect

Thanks for the help!!

- Mike


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top