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.
Related
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.
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);
hello i am using MYSQL for my data base. When i use Mongolian font. it saves in MYSQL like ????. How to correct this?
When i save in English it looks ok but in Mongolian it looks like ??? in the picture.
In the screen it also looks like ????
my MYSQL insert code is:
$sql ="INSERT INTO core_network (system_name,sub_cat,location,alarmtype,severity,start_time,end_time,reason,shift_operation)
VALUES ('$S_name','$subcategory','$city','$a_type','$severity','$S_time','$F_time','$reason','$shift_operation')";
how to correct?
$connection = mysql_connect($host1, $user, $pass)
or die("Could not connect: ".mysql_error());
mysql_select_db($database1,$connection)
or die("Error in selecting the database:".mysql_error());
after connect to DB and before use "SELECT" use this code :
mysql_query("SET CHARACTER SET utf8",$connection);
You need to set the collation of the columns to an UTF-8 one like utf8_general_ci.
Also you have to make sure that the connection charset is UTF-8 as well, for example by issuing
SET NAMES "utf8"
before any other SQL.
I am trying to convert a string from HTML-ENTITIES to UTF-8 and then save the encoded string in my database. The html entities are greek letters and look for example like this: νω
Now I tried thousands of different ways, starting from just using utf8_encode or html_entity_decode until now I came across the function mb_convert_encoding().
Now the really weird thing is that when converting my string and then outputting it, it is correctly encoded to utf-8, but when inserting this string into my database I end up getting something like: ξÏνω.
This is the code for the encoding:
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
......
while($arr = $select->fetch_array(MYSQLI_ASSOC))
{
$text = $arr["greek"];
$result = mb_convert_encoding($text, 'UTF-8', 'HTML-ENTITIES');
$mysqli->query("UPDATE some SET greek = '".$result."'");
}
When outputting my query and then manually doing a sql query in phpmyadmin it works fine, so it doesnt seem to be a problem of my db. There must be some problem when transferring the encoded string to my database...
As you see in your script, you are instructing the browser to use UTF8. That is the first step.
However your database needs the same thing and also the encoding/collation on the tables need to be UTF8 too.
You can either recreate your tables using utf8_general_ci or utf8_unicode_ci as the collation, or convert the existing tables (see here)
You need to also make sure that your database connection i.e. php code to mysql is using UTF8. If you are using PDO there are plenty of articles that show how to do that. The simplest way is to do:
$mysqli->query('SET NAMES utf8');
NOTE The change you will make now is final. If you change the connection encoding to your database, you could affect existing data.
EDIT You can do the following to set the connection
$mysqli = new mysqli($host, $user, $pass, $db);
if (!$mysqli->set_charset("utf8")) {
die("Error loading character set utf8: %s\n", $mysqli->error);
}
$mysqli->close();
Links of interest:
Whether to use "SET NAMES"
Execute the SET NAMES 'utf8' query prior to any others.
I have a mysql database (Wamp) which uses latin1_swedish to encode characters (é, è, ...) but for an unknown reason when I display some results from the database (using an HTML page and a PHP page) the diacritics (à, é, è) are not displayed correctly.
I've already tried to change the charset of my webpage (iso 88599-1, utf-8) but it doesn't work.
echo htmlentities($variable_name);
Try to check the encoding on your files (you can do this with notapad++), in your database (you can see this in phpmyadmin), and your database connection (you can check this in your php file that connects to the db).
They should all match
If you know the character encoding that comes in and you know what goes out, you may be able to use iconv: http://www.php.net/manual/en/function.iconv.php
For example if the db is iso-8859 and you need utf-8, you call
iconv("ISO-8859-1", "UTF-8", $text);
You can't just change the character set of a table once values are stored, because the data will not be converted. You will have to convert the data using the available MySQL functions. Read more about that at http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html.
Did you set your connection to mysql db with the
SET NAMES utf8;
command?
There are several ways:
mysql_query("SET NAMES 'utf8'");
and
mysql_query("SET CHARACTER SET utf8 ");
Or, the "best";
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);