To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Database

Database Conversation regarding PHP and SQL

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-18-2007, 07:42 AM   #1
deezerd
Member
 
Join Date: Dec 2006
Posts: 32
resolved Difference between utf8_general_ci and utf8_bin

Hi there,

does anyone know the difference between these two types of mysql table collations:

utf8_general_ci and utf8_bin

Is one better than the other and if yes, can you explain why?

thanks

Last edited by deezerd; 08-18-2007 at 07:41 PM.
deezerd is offline   Reply With Quote
Old 08-18-2007, 08:57 AM   #2
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,741
utf8_bin: compare strings by the binary value of each character in the string

utf8_general_ci: compare strings using general language rules and using case-insensitive comparisons

utf8_general_cs: compare strings using general language rules and using case-sensitive comparisons

For example, the following will evaluate at true with either of the UTF8_general collations, but not with the utf8_bin collation:
Code:
Ä = A
Ö = O
Ü = U
With the utf8_general_ci collation, they would also return true even if not the same case.

None of these is inherently "better"; they simply have different functionalities. You need to choose which one best suits your particular needs.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Old 08-18-2007, 09:10 AM   #3
deezerd
Member
 
Join Date: Dec 2006
Posts: 32
thanks for your very clear answer

which makes me think to another question:

what are the consequences if utf8_bin collation doesn't evaluate at true Ä = A

I mean, will it change the way my php script work if I use some sort or str_replace functions?
For example, does it mean these two tests below will return the same result?

Code:
$string = 'Ä'; // result that would come from my table

str_replace("Ä","foo",$string); 

str_replace("A","foo",$string);
thanks
deezerd is offline   Reply With Quote
Old 08-18-2007, 09:43 AM   #4
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,741
It only changes how MySQL will process queries; it has no effect on what PHP does. So if the WHERE clause of a query says "WHERE first_name = 'Bob'", the different collations would return matches for first_name values of:

'Bob' : utf8_bin, utf8_general_ci and utf8_general_cs
'Böb' : utf8_general_ci and utf8_general_cs
'BÖB' : utf8_general_ci

Again, this is tranparent to PHP. It only affect how MySQL performs string comparisons while processing queries.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Old 08-18-2007, 04:47 PM   #5
deezerd
Member
 
Join Date: Dec 2006
Posts: 32
May I ask your opinion on something? I'm currently developping a blog application in which users could perform a search in blogs posts or post titles. In this case, it would then make sense to use utf8_general_ci, don't you think?
I'm looking for kind of a best practice for this thing, so you opinion is highly welcome
deezerd is offline   Reply With Quote
Old 08-18-2007, 06:43 PM   #6
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,741
Quote:
Originally Posted by deezerd
May I ask your opinion on something? I'm currently developping a blog application in which users could perform a search in blogs posts or post titles. In this case, it would then make sense to use utf8_general_ci, don't you think?
I'm looking for kind of a best practice for this thing, so you opinion is highly welcome
Sounds good to me.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Old 08-18-2007, 07:09 PM   #7
oVerboosT
Junior Member
 
Join Date: Jan 2004
Location: Toronto, Canada.
Posts: 3
I'd just like to say thanks as well to NogDog for that explaination too.
Cheers!
oVerboosT is offline   Reply With Quote
Old 08-18-2007, 07:17 PM   #8
deezerd
Member
 
Join Date: Dec 2006
Posts: 32
resolved

Many thanks, it's all clear for me now

Last edited by deezerd; 08-18-2007 at 07:40 PM.
deezerd is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 01:08 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.