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

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

Related

UTF8 mysql encoded database field showing only special characters in php page

I have a database field with utf8_unicode_ci encoding in MySQL. This arrangements done by the previous developer and it's store arabic language data in some special character format like this:
رنا كلبونه
I have set the headers to <meta http-equiv="Content-type" value="text/html; charset=utf-8" /> but its not showing the arabic language characters when I try to fetch the values from the database. It's only showing these special characters instead of the language.
If you are using MySQL then use below after DB connection
mysql_query("SET NAMES utf8");
If you are using MySQLi then use below after DB connection
mysqli_set_charset($connection,"utf8");
If you are using class and object of mysqli then use below
$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->set_charset("utf8");
Hope this will help !

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

UTF 8 encoding not working properly in PHP

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

PHP UTF8 Database

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();
}

Save Accents in MySQL Database

I'm trying to save French accents in my database, but they aren't saved like they should in the DB.For example, a "é" is saved as "é".I've tried to set my files to "Unicode (utf-8)", the fields in the DB are "utf8_general_ci" as well as the DB itself.When I look at my data posted through AJAX with Firebug, I see the accent passed as "é", so it's correct.Thanks and let me know you need more info!
Personally I solved the same issue by adding after the MySQL connection code:
mysql_set_charset("utf8");
or for mysqli:
mysqli_set_charset($conn, "utf8");
or the mysqli OOP equivalent:
$conn->set_charset("utf8");
And sometimes you'll have to define the main php charset by adding this code:
mb_internal_encoding('UTF-8');
On the client HTML side you have to add the following header data :
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
In order to use JSON AJAX results (e.g. by using jQuery), you should define the header by adding :
header("Content-type: application/json;charset=utf8");
json_encode(
some_data
);
This should do the trick
The best bet is that your database connection is not UTF-8 encoded - it is usually ISO-8859-1 by default.
Try sending a query
SET NAMES utf8;
after making the connection.
mysqli_set_charset($conn, "utf8");
if you use PDO, you must instanciate like that :
new \PDO("mysql:host=$host;dbname=$schema", $username, $password, array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8') );
Use UTF8:
Set a meta in your
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
When you connect to your mySQL DB, force encoding so you DONT have to play with your mysql settings
$conn = mysql_connect('server', 'user', 'password') or die('Could not connect to mysql server.');
mysql_select_db('mydb') or die('Could not select database.');
mysql_set_charset('utf8',$conn); //THIS IS THE IMPORTANT PART
If you use AJAX, set you encoding like this:
header('Content-type: text/html; charset=utf-8');
Have you reviewed http://dev.mysql.com/doc/refman/5.0/en/charset-unicode.html:
Client applications that need to
communicate with the server using
Unicode should set the client
character set accordingly; for
example, by issuing a SET NAMES 'utf8'
statement. ucs2 cannot be used as a
client character set, which means that
it does not work for SET NAMES or SET
CHARACTER SET. (See Section 9.1.4,
“Connection Character Sets and
Collations”.)
Further to that:
if you get data via php from your
mysql-db (everything utf-8) but still
get '?' for some special characters in
your browser (), try this:
after mysql_connect() , and
mysql_select_db() add this lines:
mysql_query("SET NAMES utf8");
worked for me. i tried first with the
utf8_encode, but this only worked for
äüöéè... and so on, but not for
kyrillic and other chars.
You need to a) make sure your tables are using a character encoding that can encode such characters (UTF-8 tends to be the go-to encoding these days) and b) make sure that your form submissions are being sent to the database in the same character encoding. You do this by saving your HTML/PHP/whatever files as UTF-8, and by including a meta tag in the head that tells the browser to use UTF-8 encoding.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Oh, and don't forget C, when connecting to the database, make sure you're actually using the correct character set by executing a SET NAMES charset=utf8 (might not be the correct syntax, I'll have to look up what it should be, but it will be along those lines)
PHP(.net) advises against setting charsets after connecting using a query like SET NAMES utf8 because your functionality for escaping data inside MySQL statements might not work as intended.
Do not use SET NAMES utf8 but use the appropriate ..._set_charset() function (or method) instead, in case you are using PHP.
Ok I have found a working solution for me :
Run this mysql command
show variables like 'char%';
Here you have many variables : "character_set_server", "character_set_system" etc.
In my case I have "é" for "é" in database and I want to show "é" on my website.
To work I have to change "character_set_server" value from "utf8mb4" to "latin1".
All my correct value are :
And other values are :
With theses values the wrong database accent are corrected and well displayed by the server.
But each case can be different.

Categories