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);
?>
user123_456
  • 5,635
  • 26
  • 84
  • 140

2 Answers2

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

Vucko
  • 20,555
  • 10
  • 56
  • 107
  • 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? – user123_456 Mar 07 '13 at 00:12
  • @denonth looking at your php code, do you have `mysql_select_db` after the connection? – Vucko Mar 07 '13 at 00:13
  • no this is the code that is used for testing. there is no `mysql_select_db` – user123_456 Mar 07 '13 at 00: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)](http://superuser.com/questions/292086/how-can-i-enforce-so-notepad-uses-utf-8-every-time-i-create-a-new-file) ? – Vucko Mar 07 '13 at 00:20
  • Done exactly as you wrote it..this is inserted instead `ÄćÄž` – user123_456 Mar 07 '13 at 00:21
  • @denonth are you using HTML5 ? If not , put `` in your documents (.php) ``. – Vucko Mar 07 '13 at 00:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25732/discussion-between-vucko-and-denonth) – Vucko Mar 07 '13 at 00:26
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">
Jeffrey Neo
  • 3,693
  • 2
  • 26
  • 30