PHP UTF8 Database - php

I have a database which is encoded in UTF8_bin.
Whenever I try to echo something on that database I get questionmarks instead of letters. Anyone knows a solution for that? I think it is important to mention that if I do echo to a word in UTF8 it is just fine. The problem is getting the data from the database.

Please check whether you have followed these steps.
-> db collation must be utf8_general_ci
-> then collation of the table with language has to be utf8_general_ci
-> in your php connection script put header('Content-Type: text/html; charset=utf-8');
-> in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-> after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");
Then check the connection like this,
if(!mysqli_set_charset($conn, 'utf8')) {
echo 'the connection is not in utf8';
exit();
}

Related

why does my html not display special characters taken from my database

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'");

UTF8 woes with mysql/php

I've got a mysql table with some imported data, in particular one value is Sinn Féin.
The character set used for my database is utf8_general_ci. The data displays fine in phpMyAdmin. In my site, I've used the PHP header header("Content-type: text/html; charset='utf-8'");. I've got <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> in my <head>.
My data still comes up as Sinn F�in. I've tried using utf8_decode, but that doesn't help. What am I doing wrong?
Аfter mysql_connect() add this line:
mysql_query ("SET NAMES utf8");
Try this:
$dbc="database connection";
mysql_query("SET NAMES utf8",$dbc);

Php page can't recognize "ø" from mysql value

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

Latin characters in phpMyAdmin with UTF-8 collation

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

mysql encoding problem

i have a proble, when insert something in foreign language into database.
i have set the collation of database to utf8_general_ci(try utf8_unicod_ci too).
but when i insert some text into table, it was saved like this
Õ€Õ¡ÕµÕ¥Ö€Õ¥Õ¶ Ô±Õ¶Õ¸Ö‚Õ¶
but when i read from database, text shows in correct form. it looks like that only in database.
i have set encoding in my html document to charset=UTF-8
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
and i set
mysql_query("SET NAMES UTF-8");
mysql_query("SET CHARACTER SET UTF-8");
when conecting to database.
so i think that i' ve done everything, but it still save in that anknown format.
could you help me.
thanks in advance
I believe you have to SET NAMES utf8, instead of UTF-8, in MySQL.
It looks like maybe your phpmyadmin isn't using the correct charset. In your phpmyadmin folder, open config.default.php and edit the lines
$cfg['DefaultCharset'] = 'iso-8859-1';
$cfg['DefaultLang'] = 'en-iso-8859-1';
To your chosen encoding.
It is suggested to use mysql_set_charset() instead of "SET NAMES" query, however the impact should be the same.

Categories