PHP/MySQL encoding problems. � instead of certain characters

后端 未结 6 2030
孤街浪徒
孤街浪徒 2021-01-07 01:00

I have come across some problems when inputting certain characters into my mysql database using php. What I am doing is submitting user inputted text to a database. I cannot

6条回答
  •  北海茫月
    2021-01-07 01:17

    The solutions provided are helpful if starting from scratch. Putting all possible connections to UTF-8 is indeed the safest. UTF-8 is the most used charset on the net for a variety of reasons.

    Some suggestions and a word of warning:

    • copy the tables you want to sanitize with a unique prefix (tmp_)
    • although your db-connection is forced to utf8, check you General Settings collation, change to utf8_bin if that was not done yet
    • you need to run this on the local server
    • the funny char error is mostly due to mixing LATIN1 with UTF-8 configurations. This solution is designed for this. It could work with other used char-sets that LATIN1 but I haven't checked this
    • check these tmp_tables extensively before copying back to the original

    Builds the 2 array needed for the magic:

    $chars = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "UTF-8");
    $LATIN1 = $UTF8 = array();
    while (list($key,$val) = each ($chars)) {
        $UTF8[] = $key;
        $LATIN1[] = $val;
    }
    

    Now build up the routines you need: (tables->)rows->fields and at each field call

    $row[$field] = mysql_real_escape_string(str_replace($LATIN1 , $UTF8 , $row[$field]));
    $q[] = "$field = '{$row[$field]}'";
    

    Finally build up and send the query:

    mysql_query("UPDATE $table SET " . implode(" , " , $q) . " WHERE id = '{$row['id']}' LIMIT 1");
    

提交回复
热议问题