When i enter data from a form into a sql database the code below sometimes still doesn't insiert special characters correctly.
$titel = mysql_real_escape_string($_POST['titel']);
I have no clue what it can be....
An example
Assassin’s Creed Unity –
The same character sometimes does work...
Destiny's Raids
Some more examples....
Hyrule Warriors ‘Cia’ gameplay<br>
Dragon Age: Inquisition Gameplay Features – Crafting &am<br>
NBA 2K15 – Welcome To MyPARK<br>
Sid Meier’s Civilization: Beyond Earth â€&ldq
Any tips?
Do this guidelines for a working UTF8 solution:
Use SET NAMES 'utf-8' when opening your database connection, for example:
$bd = mysql_connect(BD_SERVER, BD_USER, BD_PASSWORD);
mysql_select_db(BD_DATABASE, $bd);
mysql_query("SET NAMES 'utf8'", $bd);
Use utf8_general_ci AS your database collation.
Save your PHP files with UTF-8 encoding.
Put in headers of your pages next meta: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Related
I included this at the top of my php file:
<?php
header('Content-Type: text/html; charset=UTF-8');
?>
I did this because my file.php was not displaying "á, é, í, ó, ú or ¿" in the html file or from data queried from my database.
After I placed the 'header('Content-Type: text/html; charset=UTF-8');' line of code my html page started to understand the special characters in the html file but, data received from my database now has a black rhombus with a question mark.
The collation my database has is "utf8_spanish_ci"
at the html tag i tried to put lang=es but this never worked I also tried to put the meta tag inside the head tag
<!DOCTYPE html>
<html lang=es>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>
I also tried:
<meta charset="utf-8">
and:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
I don't know what the problem is. When I insert data directly into the data base the special characters are there but when I insert them from my file.php they appear with random characters.
Does anyone know why this is happening?
There are a couple of reasons this could be happening. It is however important that your entire line of code uses the same set of charset, and that all functions that can be set to a specific charset, is set to the same. The most widely used one is UTF-8, which is the one I'm suggesting you use.
Connection
You also need to specify the charset in the connection itself.
PDO (specified in the object itself):
$handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"));
MySQLi: (placed directly after creating the connection)
* For OOP: $mysqli->set_charset("utf8");
* For procedural: mysqli_set_charset($mysqli, "utf8");
(where $mysqli is the MySQLi connection)
MySQL (depricated, you should convert to PDO or MySQLi): (placed directly after creating the connection)
mysql_set_charset("utf8");
Database
Your database and all its tables has to be set to UTF-8. Note that charset is not the same as collation.
You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
File-encoding
It's also important that the .php file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar (Convert to UFT-8 w/o BOM). You should use UTF-8 w/o BOM.
Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.
Are you sure? And are you sure that you are retrieving your data from the data base? Having said that, most databases require you to save data in a way that is NOT exactly like your question. There is really valid security reasons for this.
You should use utf8_general_ci as a database encoding also before insert query you should run this query
Mysql_query(" SET NAMES 'utf8'");
When echoing data from the MySQL database I get strange symbols in the text. I've tried htmlspecialchars_decode(), but to no avail. The data is stored as a VARCHAR in the MySQL database and is displayed as they should when queried in MySQL workbench.
The characters include ', ë, è, é, ê, ...
How do I get these characters to display in html?
Have you declared a charset in the HTML of your page?
In HTML5 this is fine:
<meta charset="UTF-8">
Older (HTML 4.01) needs something like:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
You have encoding issues.
You need to set <meta charset="UTF-8">.
You need to run query after you set up connection to DB "SET NAMES utf8"
If you using PDO need set PDO like this new M_PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf-8", $dbuser, $dbpassword);
Also you can use this function which will encode accents to html entities
function convert($str){
return mb_convert_encoding($str, "HTML-ENTITIES", "UTF-8");
}
echo convert($YourDataFromDB);
Hope it will helps
This worked for me:
$db = Database::getInstance();
$mysqli = $db->getConnection();
$mysqli->set_charset('utf8mb4');
Stolen from UTF-8 all the way through
Firstly, I know this question has been asked a lot of times but all the answers I got, they say that I have to put all my encoding into UTF-8, but actually, they're all with UTF-8 and still not working!! Here is my problem:
I have a registration in my webpage, and when a user with a ñ in his name registers, it stores it in the database as ñ, so if I register with the name "ñaña", it goes to the db as "ñaña".
I have set my db table and database to utf8_general_ci, and I also have this code in my header:
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
And this code in my PDO connection:
$connection = new PDO("mysql:charset=utf8;host=$host; dbname=$dbname", $user, $password);
But it's still stored in the db as a different character... Also, all the accents like "à, é, ê, ö..." are working fine.
What am I missing?
Thanks.
You may have created your tables with a character encoding that doesn't support special. characters; that would explain what you're seeing. You can try these SQL commands to discover the charset of your database/table/column.
If your tables are encoded with something other than utf8, you can use these other SQL commands to convert them to utf8. But I'm not sure what will happen with the current data.
You can create a function to replace the values
function sanitize($string)
{
$string = str_replace('ñ', 'ñ', $string);
return $string;
}
Hope it works for you
I'm trying to pull text from a database. In the database, it displays as "♦" with no problem.
However, when I pull the record from the database, it displays on the page as a question mark (not the black box with a white ? in the middle), a plain old "end of question" question mark.
For instance:
♦ We Need To Pay You ♦
Will display:
? We Need to Pay You ?
I have the database structure setup as utf8-bin because latin didn't save them right. I've done hours of looking around, reading articles, and cannot find an answer anywhere. There has to be a way to get it to pull from the database the right way. I can insert it but can't retrieve it which makes no sense.
All help is appreciated.
Save your php file as Encode UTF-8 without BOM
set before your query: mysql_set_charset('utf8',$connect) or
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") if you are using PDO
set inside your <head> tag: <meta http-equiv="content-type"
content="text/html; charset=utf-8">
Try this in your php file:
echo utf8_encode(text) or utf8_decode(text)
And this in your html head:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
set the charset with mysql_set_charset.
bool mysql_set_charset ( string $charset [, resource $link_identifier = NULL ] )
and be sure your page content charset is utf-8
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
You can use the answer I gave here to check for the source of the problem: How to fix "Incorrect string value" errors?
In your case most likely the HTML is not UTF-8 encoded. Depending on the HTML version you are using, one of those will fit your needs: http://en.wikipedia.org/wiki/Character_encodings_in_HTML
One thing left: Check, that your IDE/Editor also is set to store the files in UTF-8
Well, you need to decide what encode you will use on your application, IMHO utf8 is the best encoding, so you need to make you IDE ou Text Editor works with UTF-8 only and tell browser that your page is UTF-8 using:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
And using a modern connection way with PDO:
<?php
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=test',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
Creating a database using UTF-8:
CREATE DATABASE "test" CHARACTER SET utf8 COLLATE utf8_general_ci
Creating a table using UTF-8:
CREATE TABLE `test` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 40 ) NOT NULL
) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
Using utf8_encode and utf8_decode is pointless, it will make your code unreadable.
Sorry my english, i'm brazilian :D
My website uses:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
And this meta:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I created my database and tables in phpMyAdmin making sure everything is set as utf8_unicode_ci (table, fields, database), even the connection collation.
When I insert some latin characters (accents, ñ and stuff like that) into the database using an standard form made in PHP, it works fine, I can display the saved data in my website no problem. But if I go to phpMyAdmin, all the latin characters are all messed up, something like ñññ.
If I try to fix that data in phpMyAdmin, then my website displays the data incorrectly, with weird symbols ���.
What in this world am I doing wrong? I've been trying to work this out for hours with no success.
Thank you all!
As #Artefacto says, this could be a problem local to phpMyAdmin.
If phpMyAdmin is fine (i.e. set to UTF-8) and the data is still showing up weird, then look at whether your database connection using UTF-8 as well?
mysql_query("SET NAMES utf8")
(or whatever you use as a database library)
might help if it isn't.
If phpMyAdmin is showing ñññ instead of ñññ, that's because it's interpreting a UTF-8 bytestream as ISO-8859-1. So your database contents are probably correct, phpMyAdmin is just showing them in a wrong manner.
I'm not familiar with the application, but you can force the browser to read the page as UTF-8 (typically View > Encoding > UTF-8).
for Scuzzu's Question there are several ways:
http://www.mysqlperformanceblog.com/2007/12/18/fixing-column-encoding-mess-in-mysql/
I would have eight more links with different tasks but this site is prevent me from saving them...
"as a spam prevention mechanism, new users can only post a maximum of one hyperlink"
mysql_query("SET NAMES utf8") solved my problem.
Problem: Characters displayed correctly in phpMyAdmin, wrong in HTML/PHP. Characters: â, î, etc.
The headers are ok (utf8), phpmyadmin displays the content in the correct form, but the website keeps showing weird question-mark characters (inside a black diamond character).
HTML code:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Solution: I've added in php (preceding any other queries):
mysql_query("SET NAMES utf8");
Other causes can lead to this behaviour; this is only one part of the solution. You may need to check content-type, web server configuration (httpd.conf), html lang / dir, charset, etc. However, question mark in a black diamond seems to be more specific to this problem/solution, in other cases are displayed 2-3 weird characters instead of the one you want.
After doing mysql_query("SET names 'utf8';");
You need also to go to mySQL and define collation as UTF-8, also!
This should solve your problem.
Good luck!
1- Edit file:
my.ini (MsWindos)
my.cnf (GNU/linux)
2- Look for [mysqld] entry and append:
character-set-server = utf8
skip-character-set-client-handshake
The whole view should look like:
[mysqld]
port=3306
character-set-server = utf8
skip-character-set-client-handshake
3- Restart MySQL service!
If phpMyAdmin is fine (i.e. set to UTF-8) and the data is still showing up weird, then look at whether your database connection using UTF-8 as well?
Just set mysql_query("SET NAMES utf8"); after mysql_connect line:
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_query("SET NAMES utf8");
I was struggled with the same problem and finally found an elegant way to check and change collation via PHP function mysqli_set_charset. Following piece of code has to be included between a $conn check and an $sql query:
if (!mysqli_set_charset($conn, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($conn)); //optional row
die();
}
I found the solution here: http://php.net/manual/en/mysqli.set-charset.php