简体   繁体   中英

php mysql special character

My table looks like this-

province_id int(11)                  
province_title char(200) utf8_general_ci 
parent int(8) int(2)

I have inserted a lot of data direct from phpMyadmin (not from php page). but when i retrieve those data and show them in php page, then there are problems with special characters.

like-

Québec shows -  Qu�bec.

my html header looks like -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

i think the problem might have happened while inserting the data. how can i convert those data in phpmyadmin? any help?

seems like you're not using utf-8 everywhere so your data got messed up at some point. depending on what exactly you're doing, you'll have to change/add one or more of the following points (most likely it's the SET CHARSET / mysql_set_charset wich you forgot):

  • tell MySQL to use utf-8. to do this, add this to your my.cnf:

     collation_server = utf8_unicode_ci character_set_server = utf8 
  • before interacting with mysql, send this two querys:

     SET NAMES 'utf8'; CHARSET 'utf8'; 

    or, alternatively, let php do this after opening the connection:

     mysql_set_charset('utf8', $conn); // when using the mysql_-functions mysqli::set_charset('utf8') // when using mysqli 
  • set UTF-8 as the default charset for your database

     CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8'; 
  • do the same for tables:

     CREATE TABLE `my_table` ( -- ... ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
  • assuming the client is a browser, serve your content as utf-8 and the the correct header:

     header('Content-type: text/html; charset=utf-8'); 

    to be really sure the browser understands, add a meta-tag:

     <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
  • and, last but not least, tell the browser to submit forms using utf-8

     <form accept-charset="utf-8" ...> 

The problem is probably the character set of your connection to MySQL. See mysql_set_charset or mysqli::set_charset

MySQL server is not configured to expect UTF-8 encoding by default from client.

So you have to open connection using

mysql_query("SET NAMES utf8");   

If you are using PDO,

$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);

If it looks ok when you see the data in phpmyadmin the problem may be with the text enconding of the file itself.

Use Eclipse (Edit » Set Encoding) or Notepad++ (Encoding) to confirm that you have the right encoding in the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM