Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
mysql select first letter
Old 05-02-2008, 01:34 AM mysql select first letter
Extreme Talker

Posts: 177
Trades: 0
I've been googling for a while and am still unsuccessful.

I'm trying to grab all the users from a table based on the first letter. Anyone have any ideas on how to do that SELECT statement?
kbfirebreather is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-02-2008, 04:20 AM Re: mysql select first letter
Mad182's Avatar
Skilled Talker

Posts: 54
Name: Madars
Location: Latvia
Trades: 0
Code:
"SELECT * FROM users WHERE username LIKE 'letter%'"
But maybe there are smarter ways, how to do it...
__________________
There's no place like 127.0.0.1

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Mad182 is offline
Reply With Quote
View Public Profile Visit Mad182's homepage!
 
Old 05-02-2008, 04:20 AM Re: mysql select first letter
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
PHP Code:
$start_letter 'A'//Make sure to sanitize this data if coming from user input

//Example sanitization assuming a URL of the form filename.php?start_letter=A
$start_letter substr(preg_replace('/[^a-z]/i','',$_GET['start_letter']),0,1);

$results mysql_query("SELECT field_names FROM table_name WHERE search_field_name LIKE '".$start_letter."%'");

if (
$results && mysql_num_rows($results) > 0) {
  while (
$a_result mysql_fetch_object($results)) {
    
//Do your thing with the results.
  
}

__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-02-2008, 11:16 AM Re: mysql select first letter
Extreme Talker

Posts: 177
Trades: 0
Is there a way to make it case insensitive?
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 11:50 AM Re: mysql select first letter
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Is there a way to make it case insensitive?
Use lower() in the SQL where clause with an strtolower() php call on the parameter.
PHP Code:
$results mysql_query("SELECT field_names FROM table_name WHERE lower(search_field_name) LIKE '".strtolower($start_letter)."%'"); 
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 05-02-2008 at 11:51 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-02-2008, 12:01 PM Re: mysql select first letter
Extreme Talker

Posts: 177
Trades: 0
So when you start with a lowercase, it checks both upper and lower, but if you start with upper, it only checks fields with upper? That seems to be the case when I've been messing around with it, but I'm not for certain.

Last edited by kbfirebreather; 05-02-2008 at 12:04 PM..
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 12:57 PM Re: mysql select first letter
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
no. The sql part lower() tells the DB to cast any content of the field to a lowercase.
Then the seed is forced to lower case too, so you don't have to care about it
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-02-2008, 02:57 PM Re: mysql select first letter
Novice Talker

Posts: 5
Name: dave patrick
Trades: 0
Instead of coding this in php you can change the collation of the field you are searching in the database eg if your collation is set to latin1_bin change it to utf8_unicode_ci the ci stands for case insensitive
dab42pat is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 05:08 PM Re: mysql select first letter
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
My code does do a case-insensitive search. No changes needed.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-02-2008, 11:14 PM Re: mysql select first letter
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
I don't think the LIKE clause in mysql is case sensitive.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-05-2008, 12:14 AM Re: mysql select first letter
Average Talker

Posts: 18
Name: TK
Trades: 0
Quote:
Originally Posted by mgraphic View Post
I don't think the LIKE clause in mysql is case sensitive.
When all else fails, consult the documentation.

http://dev.mysql.com/doc/refman/5.0/...nsitivity.html
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-05-2008, 12:20 AM Re: mysql select first letter
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
May want to check yourself before you start suggesting such stuff to a mod with 2000+ posts, zxcvbnm60...mgraphic was simply informing readers that the code provided with LIKE as the query is case-insensitive.

Besides, almost every post on a forum could be answered with 1) see the documentation, or 2) search Google, so those are useless replies. The third, not-so-useless, option is to hire a coder.

You will find many people who do not explicitly assert something, but, instead, suggest that the thing is likely. It is a matter of delivery and not accuracy.

Good luck as you familiarize yourself with the forum and welcome to WT.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-05-2008, 03:54 AM Re: mysql select first letter
Average Talker

Posts: 18
Name: TK
Trades: 0
Quote:
Originally Posted by JeremyMiller
You will find many people who do not explicitly assert something, but, instead, suggest that the thing is likely. It is a matter of delivery and not accuracy.
Pardon me JeremyMiller (and mgraphic). The above response was not directed towards mgraphic per se, but towards any readers that may be looking for definitive answers to a question. Though the regular audience of these forums may understand the customary nuances of communication that you describe, the casual reader will not. The intent of my comment was collaborative rather than correcting.

Thanks for the welcome, good sir.
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to mysql select first letter
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37845 seconds with 12 queries