EmJam EmJam - 1 year ago 108
MySQL Question

Comparing UTF-8 string from databese with input strings (PHP/SQL)

I am trying to compare two stirngs, one is from input, and other is in datebase.
I have this list of names in databases and I want to check if the name already exist in database, but problem is special characters from serbian latin (Č,Ć,Đ,Ž,Š). Connection has UTF-8 charset mysqli_set_charset($this->con, 'utf8'), and DB has utf-8_unicode_ci collation.

$names = Teachers::returnTeachers();
if(isset($_GET['idRequest']) && $_GET['idRequest'] == 1)
$firstName = trim($_GET['firstname']);
$status = "";

foreach($name as $names)
if(strtolower(trim($names['firstname'])) == $firstName)
echo "This name is already in DB";
$status = "exist";
echo "No name";
$status="not exist";

If I have name like "Novak Đoković" in DB, and I enter "Novak Đoković" in input it will say that name doesn't exist. I can't use incov() because C, Ć or Č have different meaning. Man with name Colović it's not same as man with name Čolović.

P.S. When I echo names from DB, special characters appear normal, not like question mark symbols.

EDIT : adding some code from comments

public static function returnTeachers(){
$all = [];
$rs = Conection::query("SELECT firstname FROM teachers");
while($row = $rs->fetch_assoc()) {
return $all;

Answer Source

utf8_unicode_ci is somewhat "generic", and fails to follow the specific requirements, such as what you have for Serbian.

I do not see a 'serbian' collation, but there are several that might come close enough. Look at http://mysql.rjweb.org/utf8_collations.html

Note that utf8_croatian_ci treats C and Č and Ć as separate "letters". Czech, Slovak, and Polish (and others) do some of that. Ditto for Ž and Š.

Ð is treated as a separate letter by every collation except utf8_unicode_520_ci.

So, pick one of the other collations. utf8_croatian_ci seems closest to what you need since it is the only one that treats C and Č and Ć as distinct.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download