How do I make MySQL return UTF-8?

后端 未结 2 1329
旧时难觅i
旧时难觅i 2020-12-06 04:46

I\'m using PHPUnit to validate XML output from my PHP code, but apparently I have problems with the character encoding MySQL returns. Here is the error I get from DOMDocumen

相关标签:
2条回答
  • 2020-12-06 05:09

    You have to define the connection to your database as UTF-8:

    // Set up your connection
    $connection = mysql_connect('localhost', 'user', 'pw');
    mysql_select_db('yourdb', $connection);
    mysql_query("SET NAMES 'utf8'", $connection);
    
    // Now you get UTF-8 encoded stuff
    $query = sprintf('SELECT name FROM place where id = 1');
    $result = mysql_query($query, $connection);
    $result = mysql_fetch_assoc($result);
    
    0 讨论(0)
  • 2020-12-06 05:22

    From version PHP 5.5.0 you should use

    mysqli_set_charset($connection,"utf8");
    
    0 讨论(0)
提交回复
热议问题