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
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'");
I am trying to print data from MySql database.
I have a simple problem it is showing D�lar instead of Dólar .
Although I have included
<META http-equiv="Content-Type" Content="text/html; charset=utf-8">
In my html page so can any one help me out with this
Thanks in Advance
The character set needs to be defined in a few different places:
The MySQL database
The text stored in the database might not be encoded as UTF-8. You can define a default character set as part of the create database statement:
CREATE DATABASE mydb CHARACTER SET utf8;
You can also specify per-column character sets with create table.
Within your PHP code
You'll need to tell your client-side code which encoding it should use when communicating with the database.
If you're using PDO, you can specify the character set as part of the DSN string:
$dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8';
$dbh = new PDO($dsn, $username, $password);
If you're using MySQLi, you can use the mysqli_set_charset() function/method:
$dbh->set_charset('utf8');
or:
mysqli_set_charset($dbh, 'utf8');
Alternatively, the MySQL website suggests issuing a statement after connecting to the server:
SET NAMES 'utf8';
Within the HTML output
For HTML5, you can simply add the following <meta> tag within the <head> element of your output:
<meta charset="utf-8">
I've used bellow function and its working for me
call_user_func_array('mb_convert_encoding', array(&$str,'HTML-ENTITIES','UTF-8'));
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 have a field, $name (varchar(128)) in which there's a value "Pølse Mand".
Now I want it to print something special whenever the value is "Pølse Mand"
<?php
if ($name=="Pølse Mand") { echo "123";};
?>
But for some reason this doesn't work. I also have another value named "Text box" and it works fine when I do the same with that, so it must be the "ø" that messes things up.
I assume my "ø" in the php file is somehow different from the ø in the value, even though I've tried copying it directly letter for letter in phpmyadmin.
The MySQL connection collation is utf8_unicode_ci and the collation in the table is latin1_swedish_ci. I have tried: <meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci"/> and with the swedish one, but it just doesn't work.
Are you using MySql or MySqli?
For MySQL use this code before the request:
mysql_set_charset ( 'latin7' );
or:
mysql_set_charset ( 'utf8' );
And for MySQLi:
$mysqli -> set_charset ( 'latin7' ); //Change $mysqli to your variable name
or:
$mysqli -> set_charset ( 'utf8' );
As for the <meta> tag, use:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
or:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-13"/>
Try both options and find which works best for you.
You're using UTF-8 on the client side so must tell MySQL that you use UTF-8. This is done by the MySQL command SET NAMES 'utf8' which must be send as the first query on opening a connection. Depending on your installation and on the MySQL client library you use in the PHP script this can be done automatically on each connect.
$mysqli = new mysqli("localhost", "user", "password", "db");
$mysqli->set_charset("utf8");
//
$mysql_set_charset("utf8");
try to change table encoding to utf8 and after mysql connection run query: set names utf8
Problem, simple and annoying.
Im just trying to print a list of names, collected from my mysql database.
The PHP-files are saved in utf8, the database and tables are set to use utf8. Still 'å,ä,ö', for example, outputs as �. Can't believe I'm still having this issue.
Of course, Latin1 solves the problem. The thing is that I have to use utf8 since I'm doing some json_encode for sending the data to an ajax-script.
Any idea what on earth could be wrong?
Should I convert the data to utf8 before returning it perhaps? Seems weird I should have to..
Convert utf8_general_ci to utf8_unicode_ci...
Try running SET NAMES UTF8 query after you connect to database...
function connect($server, $database, $username, $password, $charset = "UTF8"){
$link = mysql_connect($server, $database, $password);
if(!$link){
die("Unable to connect to database server.");
}
mysql_selectdb($database);
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}
}
Also make sure you have this (or via header()) in your HTML...
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Two things to do...
Make sure your HTML header is sending the correct charset:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Use utf_encode() when adding your names to the array. The line in your should be
$guests[] = array_map('utf8_encode', $row);