harkly Posted November 26, 2008 Share Posted November 26, 2008 I want to have a query that pulls names by the 1st letter only. I have a table with 4000 names in it and I want to query the last names and have only the ones that start with "A" or even "Aa" pull up. I am pretty new to this php/mysql and not sure what approach to take. I know it can be done but not sure how. Can someone give me a bit of direction? Should I use a str_split?? Quote Link to comment https://forums.phpfreaks.com/topic/134411-query-by-1st-letter-of-lastname-str_split/ Share on other sites More sharing options...
Maq Posted November 26, 2008 Share Posted November 26, 2008 if(strtoupper(substr($last_name, 0, 1)) == 'A') { //found a match } else { //do something else } the ones that start with "A" or even "Aa" "Aa" does start with A... Quote Link to comment https://forums.phpfreaks.com/topic/134411-query-by-1st-letter-of-lastname-str_split/#findComment-699751 Share on other sites More sharing options...
harkly Posted November 26, 2008 Author Share Posted November 26, 2008 Can you explain that a bit further, not sure how "strtoupper" is being used? Very new at this. Can I put it in a while? Here's my code, $search=$_GET["alphabet"]; $result = mysql_query("SELECT * FROM artist WHERE lastname like 'A'"); $count = count(0); while ($r=mysql_fetch_array($result)) { $fullName=$r["fullName"]; $lastName=$r["lastName"]; $dob=$r["dob"]; $dod=$r["dod"]; $nationality =$r["nationality"]; $choosen_medium=$r["artForm"]; $artid=$r["artid"]; //display the row echo $count++; echo ". ".$fullName.", ".$dob."-".$dod." <br><br>"; } I was thinking I might need to split my list up further then A, B and use Aa - Am, An-Az Quote Link to comment https://forums.phpfreaks.com/topic/134411-query-by-1st-letter-of-lastname-str_split/#findComment-699752 Share on other sites More sharing options...
Barand Posted November 26, 2008 Share Posted November 26, 2008 to get those beginning with "A" you need ...WHERE lastname like 'A%' " Quote Link to comment https://forums.phpfreaks.com/topic/134411-query-by-1st-letter-of-lastname-str_split/#findComment-699827 Share on other sites More sharing options...
Maq Posted November 27, 2008 Share Posted November 27, 2008 Yeah, I don't know why I posted my first solution, very inefficient. Barand's right... Should be: $result = mysql_query("SELECT * FROM artist WHERE lastname LIKE 'A%'"); Quote Link to comment https://forums.phpfreaks.com/topic/134411-query-by-1st-letter-of-lastname-str_split/#findComment-699878 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.