0

I need to insert Croatian characters in MySQL database.

I have done all most everything but I still have a problem when I want to insert some Croatian characters.

What have I done so far:

  1. I have downloaded and installed MySQL Server 5.6 hoping that I will see Croatian collation but I couldn't find it...
  2. I have set entire database and each of the columns on UTF8-default collation
  3. I have put this into my php script: header("Content-Type: text/html;charset=utf-8");
  4. I have tried all of the combinations such as (setting everything on cp1250, latin2-croatian-ci) and still no luck

Some of the inputs that are inserted in the database are: æèæžš and it should be ćčćžš..this was just for testing...

Does anyone has any idea of what I can do next? I really hope that there is some sort of solution for this particular type of problem.

And I am sure that a lot of people were hitting the wall just like me when we are speaking about Croatian characters in MySQL database.

EDIT: Code for inserting ( this is just for testing so it's simplify)

<?php
header("Content-Type: text/html;charset=utf-8");
$con=mysqli_connect("127.0.0.1:3306","root","admin","test");

 if (!$con)
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

 $sql="INSERT INTO test(test) VALUES('ćčćžš')";
 if (!mysqli_query($con,$sql))
      {
            die('Error: ' . mysqli_error($con));
      }else 
      {
        print '<script type="text/javascript">alert("ok")</script>';
      }

    mysqli_close($con);
?>
11
  • Do you run SET NAMES utf8; when connecting to mysql?
    – Wrikken
    Mar 6, 2013 at 23:57
  • just use utf-8, nothing else. Now, what's the process that leads to the insertion? php? files? inputs? phpmyadmin?
    – Sebas
    Mar 6, 2013 at 23:58
  • sorry guys I forgot to put my php code...hold on a sec... Mar 6, 2013 at 23:59
  • what is your text editor?
    – Sebas
    Mar 7, 2013 at 0:06
  • 1
    You are not storing the PHP file itself as UTF-8. That is your problem.
    – Isaac
    Mar 7, 2013 at 0:07

2 Answers 2

3

To read UTF-8 character correctly, you can create your table like this:

create table patient(
/* your columns  */
)character set utf8 collate utf8_unicode_ci;

and to insert/retrieve your data by adding set_charset('utf8') in php:

$mysqli=new mysqli($host,$user,$password,$database);
$mysqli->set_charset('utf8');
if(mysqli_connect_errno()){
    echo 'Could not connect to database. Error: '.mysqli_connect_error();
    exit();
}

$query="select * from YOURTABLE";
$result=$mysqli->query($query);

if($result->num_rows>0){
    while($row=$result->fetch_assoc()){
        //reading $row data
    }
    $result->close();
}

$mysqli->close();

and also, remember to set your html charset to UTF-8:

<meta charset="UTF-8">
1
  • $mysqli->set_charset('utf8'); this was the problem Mar 7, 2013 at 16:37
3

For Croatian do:

  1. Your tables in the database must be utf8_general_ci.
  2. Your files must be saved as utf8 (without BOM).
  3. After connecting to your database with PHP, add this query('SET NAMES utf8');.
  4. In your documents <head> tag add <meta charset="utf-8" />.

Tested and it works ( for Croatian ).

Živio

7
  • Pozdrav. I have tried what you have said and it's not working for me. I am still not getting data in the right format...any more idea? Mar 7, 2013 at 0:12
  • @denonth looking at your php code, do you have mysql_select_db after the connection?
    – Vucko
    Mar 7, 2013 at 0:13
  • no this is the code that is used for testing. there is no mysql_select_db Mar 7, 2013 at 0:15
  • @denonth does this code successfully add the ćčćžš in the table ? Did you change your database coalition to utf8_general_ci ? In your n++, did you put utf8 (without BOM) ?
    – Vucko
    Mar 7, 2013 at 0:20
  • Done exactly as you wrote it..this is inserted instead ÄćÄž Mar 7, 2013 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.