Special characters in PHP / MySQL

Have MySQL translate it automatically

$conn = mysql_connect('host', 'user', 'password');
mysql_set_charset('utf8',$conn);

http://es.php.net/manual/en/function.mysql-set-charset.php

EDIT: from your comment I gather, that this is actually encoded in latin1 so

mysql_set_charset('latin1_spanish_ci',$conn);

I'd just like to provide some more details on the solution proposed by vartec which is (depending on your MySQL installation) the most correct solution to your problem. First of all the character set / encoding issue in MySQL is a somewhat complex subject which is extensively covered in the MySQL manual Chapter 9.1 "Character Set Support". In your case especially 9.1.4. "Connection Character Sets and Collations" will be most relevant.

To make it short: MySQL must know which character set / encoding your client application (talking from the database persoective that's your PHP script) is expecting as it'll transcode all the string data from the internal character set / encoding defined at server-, database-, table- or column-level into the connection character set / encoding. 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 sent 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.

If you use PDO it's just a matter of setting a configuration parameter

$db = new PDO($dsn, $user, $password);
$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

Using mysqli changing the client character set / encoding is even more simple:

$mysqli = new mysqli("localhost", "user", "password", "db");
$mysqli->set_charset("utf8");

I hope that will help to make the whole thing more understandable.


Issue SET NAMES 'utf8' right after connecting:

$mysqli = new mysqli("localhost", "user", "password", "database");
$mysqli->query("SET NAMES 'utf8'");

I found this from somewhere and since then have been using it as whole without thinking too much.

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");