Jump to content

Database Search help


Acute Chaos

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'

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.