3
votes

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?

3
Is the character displayed correctly in your database; it might have been written wrong to begin with.chris85
They are correctly inserted in the database and when i read them they appear as I had placed themlearningbyexample

3 Answers

7
votes

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.

0
votes

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.

0
votes

You should use utf8_general_ci as a database encoding also before insert query you should run this query

Mysql_query(" SET NAMES 'utf8'");