Cannot store UTF8 characters in MySQL

前端 未结 6 1141
抹茶落季
抹茶落季 2021-01-06 14:38

Cannot find the reason why I am unable to store in a MySQL database characters like ţ, î, ş.

My table definition is:

CREATE TABLE IF NOT EXISTS `gen_         


        
6条回答
  •  执念已碎
    2021-01-06 15:27

    as I ran your script it worked for me:

    $charset = "UTF8";
    $link = mysql_connect('localhost', 'root', '') or die('connection?');
    mysql_select_db('test') or die('database?');
    if(function_exists("mysql_set_charset")){
        mysql_set_charset($charset, $link);
    }else{
        mysql_query("SET NAMES $charset");   
    }
    
    $text = 'ţ, î, ş';
    mysql_query("insert into gen_admin_words_translated (word_id, lang_id, value, needUpd) values (1, 1, '$text', 1)");
    
    $query = mysql_query('SELECT * FROM  `gen_admin_words_translated`');
    $array = mysql_fetch_array($query);
    
    print_r($array)
    

    result:

    Array
    (
        [0] => 2689
        [id] => 2689
        [1] => 1
        [word_id] => 1
        [2] => ţ, î, ş
        [value] => ţ, î, ş
        [3] => 1
        [lang_id] => 1
        [4] => 1
        [needUpd] => 1
    )
    

    things to check:

    check if your webpage is really UTF-8, maybe you have some chaset set another place.

    header('Content-type: text/html; charset=utf-8');
    

    file encoding should be also UTF-8 as it may break your characters if otherwise ..

提交回复
热议问题