0

This seems to be ever lasting problem of showing special characters in HTML.

I am using PDO with mysql and PHP, the collation in mysql is utf8_unicode_ci .

Here is this sentence and name saved in database.

"I can't go to school

When I make a select query and echo it in my HTML file ,it appears like this

“I can't go to school

I have set $PDO->exec("set names utf8"); that should make that the response received should handle the special characters.

Moreover my HTML has this on top when I view the viewsource via browser

<meta content="charset=utf-8">

I suppose there could be two reasons, either the data is not fetched properly or it is fetched properly but not displated properly in View,

how can I solve/debug that ?

PHPLove
  • 65
  • 6
  • Did you try `new PDO("mysql:host=localhost;dbname=DB;charset=UTF8");`? – DavidDomain Jul 03 '15 at 18:44
  • @DavidDomain this probably does the same thing , $PDO->exec("set names utf8");, but let me try the one you mentioned too. – PHPLove Jul 03 '15 at 18:45
  • What PHP version are you on? To use `charset=UTF8` in constructor you need to run v.5.3.6 or above. Prior to PHP 5.3.6 you had to do manually as you did. – DavidDomain Jul 03 '15 at 18:51
  • @DavidDomain still sam – PHPLove Jul 03 '15 at 18:56
  • Last option i can think of is driver specific one for `mysql`. Here you go `new PDO( 'mysql:host=mysql.example.com;dbname=example_db', "username", "password", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));`, hope this does the trick. – DavidDomain Jul 03 '15 at 18:57
  • @DavidDomain much better but now it shows like this I can't go to school, it ate my " – PHPLove Jul 03 '15 at 19:11
  • @Marc B , I wont regard it as duplicate , the other question emphasizes on utf8mb4 – PHPLove Jul 03 '15 at 19:12
  • @SmartMindx: doesn't matter what charset you settle on, it just has to be the SAME charset throughout, unless you put charset translation logic at the "borders" of each stage. – Marc B Jul 03 '15 at 19:12
  • @MarcB I would like to ask a short question, I dont want to open another thread for this, lets save I have to save word U'SA in database, How we should save this exactly, U'SA or U\'SA? just give me short answer how google, facebook , SO stores and eager to know the official way . thnx – PHPLove Jul 03 '15 at 19:34
  • @DavidDomain sorry for pinging, but I can add just one user on comments, please see the short question above, – PHPLove Jul 03 '15 at 19:35
  • @SmartMindx: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Marc B Jul 03 '15 at 19:35
  • @MarcB my question was something else, I am also using PDO with prepared queries, my question was about end result, In DB, what should be the state of data, U'SA or U\'SA ? – PHPLove Jul 03 '15 at 19:42
  • if you have `insert ... values('U\'SA')`, then you'll get `U'SA` in the table. escaping is like wrapping paper. the db "unwraps" the data for insertion into the table. – Marc B Jul 03 '15 at 19:44
  • `“` is the mojibake for `“`. - The bytes you have in the client are correctly encoded in utf8 (good). - You connected with `SET NAMES latin1` (or `set_charset('latin1')` or ...), probably by default. (It should have been `utf8`.) - The column in the table was declared `CHARACTER SET latin1`. (Or possibly it was inherited from the table/database.) (It should have been `utf8`.) – Rick James Jul 04 '15 at 04:35

1 Answers1

0

Try change your sentence

"I can't go to school

to this

\"I can\'t go to school
Jorius
  • 204
  • 1
  • 3
  • 14