MySQL, HTML, and PHP character conflicts - php

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

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

Insert an Arabic text MySQL

I'm trying to store an Arabic text to the table, I searched a lot but I didn't find a solution that worked for me, so this is what I got:
$en = "OK";
$ar = "حسناً";
$link->query("INSERT INTO words (en,ar) VALUES ($en,$ar)");
The problem is when I insert it, the Arabic text looks like حسناً, my table's collation and MySQL's are utf8_general_ci, so is my database's, I also have mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');, but it doesn't work.
I recently had the same issues myself.
Here's a few pointers:
ALL attributes must be set to ut8 (collation is NOT the same as charset)
Save the document as UTF-8 (If you're using Notepad++, it's Format -> Convert to UFT-8)
The header in both PHP and HTML should be set to UTF-8 (HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and PHP: header('Content-Type: text/html; charset=utf-8');
Upon connecting to the databse, set the charset ti UTF-8 there as well, like this: $link->set_charset("utf8"); (directly after connecting)
Also make sure your database and tables are set to UTF-8, you can do that like this:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Remember that EVERYTHING needs to be set to UFT-8 charcode, or else it'll insert stuff like "حسناً". Hope this helped!
First thing is the database, table, and column. See if utf8 is set:
utf8_general_ci or utf8_unicode_ci
Also, the connection collation: utf8_general_ci
In PHP, after I connect with mysqli, I issue this:
$conn->query("SET NAMES 'utf8'");
And the web page output always jams in:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Here is a solution that worked for me:
when you create the database choose the collation for: utf8_unicode_ci
when you connect to the database from php use the following character set:
$dsn = "mysql:host=localhost;dbname=record;charset=utf8";

PHP special Characters

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" />

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

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