Acute Chaos Posted June 21, 2011 Share Posted June 21, 2011 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. Link to comment https://forums.phpfreaks.com/topic/240005-database-search-help/ Share on other sites More sharing options...
AbraCadaver Posted June 21, 2011 Share Posted June 21, 2011 The % is a wildcard so don't use it before the variable: $sql="SELECT id, f_name, l_name FROM user WHERE f_name LIKE '$letter%' OR l_name LIKE '$letter%'"; Link to comment https://forums.phpfreaks.com/topic/240005-database-search-help/#findComment-1232846 Share on other sites More sharing options...
Acute Chaos Posted June 21, 2011 Author Share Posted June 21, 2011 Rofl - Thanks - One of these days I'll get a handle on this but my god their is a lot to learn. Thanks a lot and holy wicked fast!! I'm sure I'll be back soon with another problem, hopefully as simple!! Link to comment https://forums.phpfreaks.com/topic/240005-database-search-help/#findComment-1232853 Share on other sites More sharing options...
Drummin Posted June 21, 2011 Share Posted June 21, 2011 I would think you need to use substring to get first letter. Hey what do I know. $sql="SELECT id, f_name, l_name FROM user WHERE f_name LIKE SUBSTRING('$letter',0,1) OR l_name LIKE SUBSTRING('$letter',0,1)"; Link to comment https://forums.phpfreaks.com/topic/240005-database-search-help/#findComment-1232861 Share on other sites More sharing options...
TeNDoLLA Posted June 21, 2011 Share Posted June 21, 2011 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. Link to comment https://forums.phpfreaks.com/topic/240005-database-search-help/#findComment-1232863 Share on other sites More sharing options...
AbraCadaver Posted June 21, 2011 Share Posted June 21, 2011 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' Link to comment https://forums.phpfreaks.com/topic/240005-database-search-help/#findComment-1232867 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.