PHP/MySQL encoding problems. � instead of certain characters

后端 未结 6 2089
孤街浪徒
孤街浪徒 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:21

    As mentioned by others, you need to convert to UTF8 from end to end if you want to support "special" characters. This means your web page, PHP, mysql connection and mysql table. The web page is fairly simple, just use the meta tag for UTF8. Ideally your headers would say UTF8 also.

    
    

    Set your PHP to use UTF8. Things would probably work anyway, but it's a good measure to do this:

    mb_internal_encoding('UTF-8');
    mb_http_output('UTF-8');
    mb_http_input('UTF-8');
    

    For mysql, you want to convert your table to UTF8, no need to export/import.

    ALTER TABLE table_name CONVERT TO CHARACTER SET utf8
    

    You can, and should, configure mysql to default utf8. But you can also run the query:

     SET NAMES UTF8
    

    as the first query after establishing a connection and that will "convert" your database connection to UTF8.

    That should solve all your character display problems.

提交回复
热议问题