从mysql解码为UTF8

时间:2015-12-25 13:27:11

标签: php mysql

我来自捷克共和国,我遇到解码问题,我将产品数据库导入MYSQL,产品名称使用代码(可能是UTF-8)存储在数据库中。 在php中,我使用public void internalFrameActivated(InternalFrameEvent e) { String user = "root"; String pass = "pass"; String schema = "db"; String loggedInUser = Login.getLoggedInUser(); try { Connection conn = MySql.getConnection(user, pass, "/" + schema); try { String q = "select date from last_login where username=?"; PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(q); stmt.setString(1, loggedInUser); ResultSet rs = stmt.executeQuery(); if (rs.next()) { lastLogin.setText(rs.getString(1)); } else { JOptionPane.showMessageDialog(rootPane, "Unable to retrieve information for user: " + loggedInUser + "!"); } } catch (Exception ex) { JOptionPane.showMessageDialog(rootPane, "Error in Query: " + ex.getMessage()); } } catch (Exception ex) { JOptionPane.showMessageDialog(rootPane, "Error in Database Connection: " + ex.getMessage()); } } 进行解码,但可能是错误的编码。有人有想法吗?谢谢。 例如:

  

112 111 109 101 114 97 110 269 101 32 115 237 116 .... - > “pomeranesít1k”

而不是

  

“pomerančesíť1k”

  

80 114 97 382 115 107 225 32 48 46 53 108 - > Pra~ská0.5

而不是

  

“Pražská0.5”

1 个答案:

答案 0 :(得分:0)

在MYSQL连接之后,您必须选择MYSQL字符集函数,如:

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link); // you need this