Jump to content

Recommended Posts

I am building a staff directory with little knowledge of what I'm doing and piecing together examples I have found online.  Because I am a noob I can't get to where I want to go with this one.

 

To search by 'Letter' I have a bunch of anchors coded at the page top

e.g.  <a  href="?by=A">A</a>

 

 

Then in the php I have:

if(isset($_GET['by'])){ 

$letter=$_GET['by'];

//  - - - database connection stuff - - -

$sql="SELECT  id, f_name, l_name FROM user WHERE f_name LIKE '%" . $letter . "%' OR l_name LIKE '%" . $letter ."%'"; 

$result=mysql_query($sql); 

$numrows=mysql_num_rows($result); 

echo  "<p>" .$numrows . " results found for " . "<i>" ."'" . $letter . "'". "</>" . "</p>";

//  - - - and then the loop that displays it - - -

 

 

My Problem is this - If I search for 'A' for example, every name in the database with a letter anywhere in it comes up.  I only want names beginning with the letter 'A' to come up.

 

Thanks for any help you can give me!

I really appreciate it.

If this is a bad way of going about it, let me know that too please and hopefully point me in a brighter direction.  :D

Link to comment
https://forums.phpfreaks.com/topic/240005-database-search-help/
Share on other sites

No need to use substring, AbdaCadavers version works just fine and no need to use extra functions in the query which might make it a bit faster.

 

Yes, and for the substring() to work it would be:

 

WHERE SUBSTRING('f_name',0,1) LIKE '$letter' OR SUBSTRING('l_name',0,1) LIKE '$letter'

Or just:

WHERE SUBSTRING('f_name',0,1) = '$letter' OR SUBSTRING('l_name',0,1) = '$letter'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.