I've some problems with special characters in PHP/MySQL (I don't know which one).
Then, I've data stored in a database,using php. I ensured before storing data to add this code:
header('Content-type:text/html; charset=utf-8');
$link=mysqli_connect("host","user","","db") or die(mysqli_error($link));
mysqli_query($link,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';SET NAMES utf8");
I also ensured Apache uses the correct charset:
AddDefaultCharset utf-8
In addition I also tried to add on every html page the meta tag:
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
Further, I saved all my php and html files in UTF-8 format.
I noticed that if I write a character like ò in a page and display that page it is correctly displayed. If I get a string query from a database containing ò, it doesn't display correctly, but if I change the code to:
header('Content-type:text/html; charset=iso-8859-1');
the query retrieved from the DB is correctly displayed, a normal writing not.
So,the problem is on mysql charset? How could it be if I setted it with the previous instructions?
try to change your column where you store this date to utf8 also
here some codes you may use.
Change the character-set/collation (database):
ALTER DATABASE db_name DEFAULT CHARACTER SET utf8;
Change the character-set/collation (table):
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8;
Change the character-set/collation (columns):
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
Try using http://fi2.php.net/mysqli_set_charset function instead of the mysqli_query() you are using now.
Related
I have a database which has the latin1 default characterset - info obtained by running the following statement:
SELECT default_character_set_name FROM information_schema.SCHEMATA
WHERE schema_name = "schemaname";
The default character set for each table and column in this database is set to utf8.
When I look at the data in the tables I can see data is stored as utf8 e.g the currency symbol € is stored in the table as €. Similarly apostraphes are stored as ’ etc.
On the web frontend I have the following meta tag and so the characters render correctly.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
However I'm also seeing a lot of � symbols on the webpage which I don't see inside the database?
When I change the database connection to include the charset utf8 as follows: mysql:host=myhost;dbname=mydatabase;charset=utf8, the diamond symbols disappear but then all the other utf8
characters revert to exactly how they are saved in the database e.g. the € symbol renders as € on the webpage?
Why is this happening?
How do I fix this and also change character set to utf8mb4?
Any help appreciated.
* UPDATE *
Tried the following steps:
for the database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Not sure if Step 3 is necessary since when I do SHOW CREATE TABLE after step 2, whilst the definition doesn't display the column charset it does display the default charset for the table as utf8mb4. As a sanity check I did run step 3 on one of the tables columns but it makes no difference - € is being rendered on the page as € with db connection as follows:
`mysql:host=myhost;dbname=mydatabase;charset=utf8mb4`
I had to run the following on each column I wanted converting which seems to fix some issues
UPDATE tbl_profiles SET profile =
convert(cast(convert(profile using latin1) as binary) using UTF8MB4);
but still seeing characters such as Iâm and «Âand ⢠rendered on the webpage
Any ideas?
* UPDATE 2 *
After running steps 1 and 2 above I have a table column as follows:
`job_salary` VARCHAR(150) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
The following query on this column returns the following result:
SELECT job_salary FROM tbl_jobs WHERE job_id = 2235;
€30,000 plus excellent benefits
I execute the following statement on this column:
UPDATE tbl_jobs SET job_salary = CONVERT(BINARY(CONVERT(job_salary USING latin1)) USING utf8mb4);
But I get the following error which means some other record has a invalid utf8mb4
Invalid utf8mb4 character string: '\x8010000 to \x8020000 Per: annum'
First, let's discuss the Mojibake of the Euro sign. All of this applies to both utf8 and utf8mb4, since the Euro is encoded the same way and there is.
It is very likely that the data was initially stored incorrectly. If you can get back to the INSERT program, let's check for:
The bytes to be stored need to be UTF-8-encoded. What was the client programming language? Where did the data come from?
The connection when INSERTing and SELECTing text needs to specify utf8 or utf8mb4. Do you have the connection parameters?
The column needs to be declared CHARACTER SET utf8 (or utf8mb4). It sounds like this was always correct.
HTML should start with .
What is currently in the table?
SELECT col, HEX(col) FROM ... WHERE ...
A correctly stored Euro sign (€) should have hex E282AC. (Interpreting that as latin1 yields €.
If instead, you see hex C3A2E2809AC2AC, you have "double encoding", and the display is probably €.
I have identified several possible fixes, but have not yet determined which applies in your case. The likely candidate is
CHARACTER SET utf8mb4 with double-encoding:
To verify it (before fixing it), please do something like:
SELECT col,
CONVERT(BINARY(CONVERT(col USING latin1)) USING utf8mb4),
HEX(
CONVERT(BINARY(CONVERT(col USING latin1)) USING utf8mb4)
)
FROM ...
WHERE ...
Do not apply a fix on top of another fix. I have struggled for a long time to decipher how character set problems occur and what to do to 'fix' a single problem. But when the wrong fix is applied, I am at a loss to unravel the mess.
I would like to export my database with emoji, but I have a problem with the export. When I exported my table, the emoji are replaced by "?".
For example :
When I export, and import, I have this :
I checked my table (utf-8) :
I use Sequel Pro to export and import.
But if I tried with DataGrip, and I have directly the "?", I never see the emoji :
before you run the queries, run
set names utf8mb4;
Why?
In short.
First, Emoji usually takes four bytes, however, mysql's utf8, an alias for for utf8mb3, using one to three bytes(i.e., max byte three), which could not understand an Emoji char. As such, you see a '?' in your result. utf8mb4 can do the job since it requires a maximum of four bytes per multibyte character.
Second, set names utf8mb4 will set three session variable, e.g.,
SET character_set_client = utf8mb4;
SET character_set_results = utf8mb4;
SET character_set_connection = utf8mb4;
which will coordinate the barrier between server, client and results char set, so we can view the Emoji correctly.
For more information, you can find in the doc
https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html
https://dev.mysql.com/doc/refman/8.0/en/set-names.html
I retrieve datas from mysql
This is normally which i have in db
This is normally i have in db
Seçimler, Şirketler ve Siyasi Partiler
it prints
Se�imler, ?irketler ve Siyasi Partiler
I use sql yog and change some preferences in my db
i set Charset to UTF8 and Collation is utf8_turkish_ci
but still retrieve datas like that
Se�imler, ?irketler ve Siyasi Partiler
why? What's the problem ?
this problem sounds like you've missed to specify a character encoding somewhere. to solve this, simply make sure you've set character encoding to utf-8 everywere (it doesn't actually need to be utf-8, just the same everywhere - but if you've messed up something and need to change some places anyway, i'd strongly recommend using utf-8):
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 afteropening the connection:
mysql_set_charset('utf8', $conn);
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" ...>
You need to mention Charset to UTF8 in your selection query too.
SET NAMES 'utf8';
CHARSET 'utf8';
What made the difference for me was to add these lines:
collation_server = utf8_unicode_ci
character_set_server = utf8
to my.ini file (at the end of the document) I have tried everything else but they all failed until I made this change. It's really been a great help. Thanks oezi.
I added
$this->db->exec("SET NAMES 'utf8'");
in connection
and added
charset utf8 to my procedure parameters
Sample
PROCEDURE `up_users`(IN `lid` INT, IN `lfirstname` VARCHAR(100) CHARSET utf8, IN `llastname` VARCHAR(100) CHARSET utf8, IN `lstatus` VARCHAR(1) CHARSET utf8, IN `lpassword` VARCHAR(255) CHARSET utf8)
am using MySQL version 5.1.49 and I have not enabled UTF8 character encoding. The default character-set for MySQL is latin1. How can I change it show UTF8 characters?
Even when I query a table using Workbench I get 'NULL' in name section which I want, should display mutibyte characters.
ALTER DATABASE DEFAULT CHARACTER SET utf8
and for each table:
ALTER TABLE SomeTableName DEFAULT CHARACTER SET utf8
also if you will be viewing them from a webpage the HTML need this:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
and if you are using PHP, use htmlspecialchars() to display the values like this:
echo htmlspecialchars($row['some_field'], ENT_COMPAT, 'UTF-8');
and (again only for PHP) do this after mysql_connect():
mysql_query("SET NAMES 'utf8'");
I am trying to retrieve a special Greek character µ from a MySQL database but in HTML it is
showing this �.
My HTML charset: content="text/html; charset=windows-1252"
In addition, after it retrieves the character it also need to be able to use the character to compare back in the database.
I've tried putting the HTML code in the database like μ then it is not able to compare back to the database because HTML turns it into µ.
How can I do this appropriately?
First, I would recommend using utf8 as your charset.
Start by switching the default for the database:
ALTER DATABASE db_name CHARACTER SET utf8;
Next, to switch your tables over to utf8 apply this SQL:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
which you will have to do for each table.
Finally, and quite unfortunately, you have to know convert all the character data columns that were made under the old charset (varchar, text, etc.). You have to do this by way of first casting to a BLOB so that the characters already stored are not mangled:
alter table tbl_name change col_name col_name LONGBLOB;
alter table tbl_name change col_name col_name LONGTEXT CHARACTER SET utf8;
Wordpress publishes a column type by column type guide on how to do this step: converting database charset columns.
Then, finally update the charset declaration in your xhtml (or without the ending / if it is html):
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
This process is arduous, but will convert the database. It is much easier to start a db in utf8.
This is all assuming MySQL 5, for which you can see the Mysql 5 ALTER TABLE reference.
Try this on your table containing the special chars (based off of this post):
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
See Shelhamer's answer, but you also need to set the db connection to utf8. You can either use mysql_set_charset('utf8'); right after you connect to the DB. Alternatively run the following query after your're connected:
SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';
however, i would still recommend you use htmlentities() when displaying your content on a website to make sure is's all valid HTML. Firefox tends to barf if you mix up the encoding.
in php use utf8_encode() before saving it to the database.
It will solve the problem