PHP & MySQL - Problems with UTF-8 encode - php

I have a form with a input that accept UTF-8 characters:
<form method="post" action="faz-personagem.php" accept-charset="UTF-8">
<div class="row">
<label for="nome">Nome</label>
<input type="text" name="nome">
</div>
...
<button type="submit">Enviar</button>
</form>
And a script that send the data to a database:
<?php
header("Content-Type: text/html;charset=UTF-8");
$conexao = mysql_connect('localhost', 'root', 'pass');
mysql_select_db('pan-tactics');
$nome = $_POST['nome'];
$nome = utf8_encode($nome);
$sql = "INSERT INTO personagens VALUES";
$sql .= "('$nome')";
$resultado = mysql_query($sql);
echo 'Personagem criado com sucesso.';
mysql_close($conexao);
?>
I also have specified in the creation of the database the collation utf8_unicode_ci and yet what I get is wrong special characters:
What can I do to fix it?

Keep in mind that collation is not the same as charset. You need to set your database and table to be in UTF-8 encoding. This can be done with running this SQL command (only need to be done once).
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Furthermore, you should set the mysql_* connection to UFT-8. This code should be placed directly after connecting to your database.
mysql_set_charset("utf8");
I see you already set the PHP header to UTF-8 as well, so that's good.
Keep in mind that usage of mysql_* functions are deprecated and no longer maintained; you should switch to PDO or MySQLi for security reasons.
If neither of these steps helped you, you may need to save the document itself as UTF-8 w/o BOM, this can be done in Notepad++ as Format -> Convert to UFT-8 w/o BOM.

i had the same problem.
This was my solution:
$con = mysqli_connect('localhost', 'secretuser', 'secret', 'mydb');
mysqli_set_charset($con, "utf8mb4");
in your case:
$conexao = mysql_connect('localhost', 'root', 'pass');
mysqli_set_charset($conexao, "utf8mb4");
Maybe this will help someone who comes across this old post.

Set your connection with the database to utf-8:
mysql_set_charsethttp://php.net/manual/en/function.mysql-set-charset.php
U should use mysqli_ functions or PDP rather then mysql_ functions!

Check your collation of your database (including columns and tables).
Also, try adding mysql_query("set names 'utf8'"); after the connection to database in your code.

Were you expecting João for the first name? If so, you have "Mojibake".
João is even worse -- It looks like you Mojibaked João to get João, then Mojibaked that to get João.

Related

MySQL and PHP: UTF-8 with Cyrillic characters [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I'm trying to insert a Cyrillic value in the MySQL table, but there is a problem with encoding.
Php:
<?php
$servername = "localhost";
$username = "a";
$password = "b";
$dbname = "c";
$conn = new mysqli($servername, $username, $password, $dbname);
mysql_query("SET NAMES 'utf8';");
mysql_query("SET CHARACTER SET 'utf8';");
mysql_query("SET SESSION collation_connection = 'utf8_general_ci';");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE `c`.`mainp` SET `search` = 'test тест' WHERE `mainp`.`id` =1;";
if ($conn->query($sql) === TRUE) {
}
$conn->close();
?>
MySQL:
| id | search |
| 1 | test ав |
Note: PHP file is utf-8, database collation utf8_general_ci
You are mixing APIs here, mysql_* and mysqli_* doesn't mix. You should stick with mysqli_ (as it seems you are anyway), as mysql_* functions are deprecated, and removed entirely in PHP7.
Your actual issue is a charset problem somewhere. Here's a few pointers which can help you get the right charset for your application. This covers most of the general problems one can face when developing a PHP/MySQL application.
ALL attributes throughout your application must be set to UTF-8
Save the document as UTF-8 w/o BOM (If you're using Notepad++, it's Format -> Convert to UTF-8 w/o BOM)
The header in both PHP and HTML should be set to UTF-8
HTML (inside <head></head> tags):
<meta charset="UTF-8">
PHP (at the top of your file, before any output):
header('Content-Type: text/html; charset=utf-8');
Upon connecting to the database, set the charset to UTF-8 for your connection-object, like this (directly after connecting)
mysqli_set_charset($conn, "utf8"); /* Procedural approach */
$conn->set_charset("utf8"); /* Object-oriented approach */
This is for mysqli_*, there are similar ones for mysql_* and PDO (see bottom of this answer).
Also make sure your database and tables are set to UTF-8, you can do that like this:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
(Any data already stored won't be converted to the proper charset, so you'll need to do this with a clean database, or update the data after doing this if there are broken characters).
If you're using json_encode(), you might need to apply the JSON_UNESCAPED_UNICODE flag, otherwise it will convert special characters to their hexadecimal equivalent.
Remember that EVERYTHING in your entire pipeline of code needs to be set to UFT-8, otherwise you might experience broken characters in your application.
In addition to this list, there may be functions that has a specific parameter for specifying a charset. The manual will tell you about this (an example is htmlspecialchars()).
There are also special functions for multibyte characters, example: strtolower() won't lower multibyte characters, for that you'll have to use mb_strtolower(), see this live demo.
Note 1: Notice that its someplace noted as utf-8 (with a dash), and someplace as utf8 (without it). It's important that you know when to use which, as they usually aren't interchangeable. For example, HTML and PHP wants utf-8, but MySQL doesn't.
Note 2: In MySQL, "charset" and "collation" is not the same thing, see Difference between Encoding and collation?. Both should be set to utf-8 though; generally collation should be either utf8_general_ci or utf8_unicode_ci, see UTF-8: General? Bin? Unicode?.
Note 3: If you're using emojis, MySQL needs to be specified with an utf8mb4 charset instead of the standard utf8, both in the database and the connection. HTML and PHP will just have UTF-8.
Setting UTF-8 with mysql_ and PDO
PDO: This is done in the DSN of your object. Note the charset attribute,
$pdo = new PDO("mysql:host=localhost;dbname=database;charset=utf8", "user", "pass");
mysql_: This is done very similar to mysqli_*, but it doesn't take the connection-object as the first argument.
mysql_set_charset('utf8');
Solution:
mysql_query("SET NAMES 'utf8';"); > $mysqli->set_charset('utf8');

How to insert an hebrew value into a mysql db in php

I'm trying to insert an hebrew value into my mysql db, instead of hebrew the values looks like that.
שדגשדכעשד
The collation of the table is latin1_swedish_ci by default, I tried also to change to utf-8_general_ci, hebrew_bin, hebrew_general_ci but the result is still the same.
In my code I'm using of course the meta tag to configure the charset:
<meta charset="UTF-8">
And before my php query I added this line:
mysql_query("SET NAMES utf8");
I'm viewing the result in the phpmyadmin.
I have solved my Hebrew language problem. It was a database and table row/field encoding issue. Here is the solution I used. I took help from another answer and the link is given below, in case anyone needs it.
The database collation has to be utf8_general_ci.
The collation of the table with Hebrew has to be utf8_general_ci.
In the PHP connection script put
header('Content-Type: text/html; charset=utf-8');
In the xhtml head tag put
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If you are using MySQLi put this code in the connection script after selecting the database:
mysql_query("SET NAMES 'utf8'");
If you are using PDO, put
$conn->query("SET NAMES 'utf8'");
The first answer helped me and I took it from there
Set charset to achieve the solution
While creating the database
CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
Or if the database is already created
CREATE TABLE table_name(
...
)
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
OR while writing query
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
$re = mysql_query('SHOW VARIABLES LIKE "%character_set%";')or die(mysql_error());
while ($r = mysql_fetch_assoc($re)) {var_dump ($r); echo "<br />";}
Check the collation_connection:
show variables like '%collation%'
you should make sure that:
you set utf-8 in php
you use utf-8 in the connection
your table is defined as utf-8_general_ci
the specific field is defined as utf-8_general_ci
Then you should be able to view Hebrew, or any other language, correctly in phpadmin
what finally helped me is to add the charset to the connection:
{"mysql:host=$host;dbname=$db;charset=utf8"}
I would say, in order to make sure that the values are passed well to the database I would add a die statment before inserting to the database and print the value, example:
die($_POST['thevalue']);
//insert to database.
//...
If it goes well, then the problem is on the database side, on the database I would try with this collation
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
as per http://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html suggest.
But if it fail on the php side, reason can be, the server does not support Hebrew, make sure that on the html output document you use the correct metatag with
...
<meta charset="ISO 8859-8">
...
Let us knows how it proceed, good luck :)
For future use, if you have this issue and you are using PDO and not mysqli, you will need to do it like this:
The Database (all of it) collation has to be utf8_general_ci.
Set the collation of the table with Hebrew also to utf8_general_ci.
In the HTML "head" tag, add this: < meta charset="utf-8" >
In your PHP connection file, add after the connection Query this line: conn->exec("set names utf8");
if you are using classes, then you will probably have the "conn" as a variable.
in that case, you will have to use it like this:
$this->conn->exec("set names utf8");
Hope this helps to future people that have this problem and using PDO.
Best of luck.
I think I figure it out:
here's my code:
$conn = mysqli_connect($dbserver,$dbuser,$dbpwd,$dbname);
if (mysqli_connect_errno()){
printf("Connection failed: %s\n" , mysqli_connect_errno());
exit();
}
printf("Initial character set: %s\n", mysqli_character_set_name($conn));
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "hebrew")) {
printf("Error loading character set hebrew: %s\n", mysqli_error($conn));
exit();
} else {
printf("Current character set: %s\n", mysqli_character_set_name($conn));
}

UTF-8 and German characters?

I have problem with German characters on my web site,
in html/php part of website i have this code to set utf-8:
<meta charset="utf-8">
in mysql, i have this code to set utf-8
SET CHARSET 'utf8';
Here is some word on German: Gemäß
Here is how that word looks in mysql table:
Gemäß
Here is how that word is shown on the site: Gemäß
What is a problem? Thanks.
I was using this code to get title:
$title = mysql_real_escape_string(htmlentities($_POST['title']));
I just override that to
$title = $_POST['title'];
At first, make sure, that you have UTF-8 characters in your database.
After that, try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.
Try SET NAMES 'utf8' or SET NAMES 'utf-8'. Some of these works fine for portuguese, probably for german too. I just can't remember which one is correct, but if it is not, an error will be produced.
you should make sure that the CONNECTION is also utf-8.
with mysqli this is done with something like this:
$connection = mysqli_connect($host, $user, $pass, $db_name);
$connection->set_charset("utf8");
Now if somehow you ended up with wrong characters in the database there is a way to make it right:
in a PHP script, retrieve the information as you do now, i.e without setting the connection. This way the mistake will be inverted and corrected and in your php file you will have the characters in the correct utf-8 format.
in a PHP script, write back the information with setting the connection to utf-8
at this point you should see the character correct in your database
now change all your read/write functions of your site to use the utf-8 from now on
in HTML5 use
<meta charset="utf-8">
in HTML 4.0.1 use
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
the results are html entity encoded as if they were processed by htmlentities(), I wonder if your variables are ibserted as received from the form or are being processed by say a wysiwg editor for instance?
Anyway, these should print fine on an html template but an html_entity_decode() should do it to.
Hope this helps
Set the data type in your database to use UTF-8 as well, this should solve the problem.
I had the same problem. which I solved by using:
if you have already created your table, you need the modify the character set as:
alter table <table name> convert to character set utf8 collate utf8_general_ci.
your tables character set is set to latin_swedish by default by MySQL.
also, you might face some problems while retrieving the data and displaying it to you page.For that include: mysql_set_charset('utf8') just below the line where you have connected your database.
eg:
mysql_connect('localhost','root','');
mysql_select_db('my db');
mysql_set_charset('utf8');
You will need to do this for php 5.x
$yourNiceLookingString =
htmlspecialchars ($YourStringFromDB, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
and for php 4.x
$yourNiceLookingString = htmlspecialchars($YourStringFromDB);

– like symbols when using text editor

When I used text editors to insert text into database, symbols like - , ' etc get converted to – like symbols. How can I avoid these symbols?
Set names to UTF8 before inserting into your database.
SET NAMES utf8
It sounds like your connection is using latin1 encoding. I struggled with the same thing for months.
http://forums.mysql.com/read.php?103,46870,47245
You need to set encoding to utf8 using method mysql_set_charset('utf8',$link);
<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
$db_selected = mysql_select_db('your_db_name', $link);
?>
And then the usual stuff you do with your connection
Check the encoding (collation) used on the database. Try changing it to utf8_general_ci or the equivalent choice on the Database you are using.

UTF-8 problems PHP/MySQL

I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.
Unfortunately I can't get it to work.
My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.
(it is special characters like æ/ø/å that doesn't work)
Hope you guys can help!
Make sure the connection to your database is also using this character set:
$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);
According to the documentation of mysql_set_charset at php.net:
Note:
This is the preferred way to change the charset. Using mysql_query() to execute
SET NAMES .. is not recommended.
See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php
Check the character set of your current connection with:
echo mysql_client_encoding($conn);
See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php
If you have done these things and add weird characters to your table, you will see it is displayed correct.
Remember to set connection encoding to utf8 as well.
In ext\mysqli do
$mysqli->set_charset("utf8")
In ext\mysql do
mysql_set_charset("utf8")
With other db extensions you might have to run query like
SET NAMES 'utf8'
Some more details about connection encoding in MySQL
As others point out, making sure your source code is utf-8 encoded also helps. Pay special attention to not having BOM (Byte Order Mark) - it would be sent to browser before any code is executed, so using headers or sessions would become impossible.
After connecting to db, run query SET NAMES UTF8
$db = new db(...);
$db->query('set name utf8');
and add this tag to header
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Are you having this error? MySql SELECT UNION Illegal mix of collations Error? Just set you entire mysql to utf 8 then
SET character_set_connection = utf8;
Try this after connecting to mysql:
mysql_query("SET NAMES 'utf8'");
And encode PHP document in UTF-8 without BOM.
I had the same problem but now its resolved. Here is the solution:
1st: update ur table
ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
2nd:
add this in the head section of the HTML code:
Regards
Saleha A.Latif
Nowadays PDO is the recommended way to use mysql. With that you should use the connection string to set encoding. For example: "mysql:host=$host;dbname=$db;charset=utf8"

Categories