0

I have a site that is storing spanish characters in a MySQL DB. All tables and DB are configured UTF-8 and is identically loaded onto my local WAMP setup and to the hosting account server as much as I can reasonably tell.

Spanish characters display perfectly locally but on the live server the dreaded question mark symbol is displayed for the accented characters. Only the text generated dynamically from the DB is affected so it would be reasonable to assume that this is somehow related to my queries, connection or DB in some way.

<meta charset="utf-8">
 <title>Untitled Document</title>
   </head>

   <select multiple class='form-control' name='location[]' id='location'>
      encoding: UTF-8<option value='Alhaur�n de la Torre'>Alhaur�n de la Torre</option>
      encoding: ASCII<option value='Artola'>Artola</option>
      encoding: ASCII<option value='Bel Air'>Bel Air</option>

Using mb_detect_encoding() my dropdown looks as above.

My header contains:

 header('Content-type: text/html; charset=utf-8');

Database is utf8_general_ci for tables, server connection collation, server charset is UTF-8 Unicode (utf8)

I have used SET NAMES utf8 in my PDO DB connection.

This is driving me crazy, why does it work locally but not on my hosting server. After much searching for a n answer I am hoping for some guidance here.

Thanks in advance

SOLVED:

In my connection script I changed this:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass);
 $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');

To this:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); 

and all works as it should.

Thanks for looking

  • I suspect UTF-8 BOM http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom – Jason OOO Mar 13 '14 at 16:15
  • I don't know what code editor you are using. For example I use notepad++. When creating any new files (php, html, css ...) I always check to be sure the document encoding is UTF-8. You can do this by clicking "Encoding" in the top menu and ensure you have a bullet in front "Encode with UTF-8 without BOM". If it is not, first click on "Convert to UTF-8 without BOM" and then save the document. If you had any oddly displaying characters left, fix them by hand and again save the document. In other editors this might be done differently. – Robert Mar 13 '14 at 16:30
  • @Robert I saved the file in Notepad++ as UTF-8 without BOM. Simply saving as UTF-8 throws a headers already sent error, so this is possibly not the problem – GalaxyTramp Mar 13 '14 at 16:34
  • It's perfectly acceptable to answer your own question. Please add your resolution to a new answer :) – Alastair McCormack Mar 15 '14 at 10:09

2 Answers2

0

SOLVED:

In my connection script I changed this:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass);
 $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');

To this:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

and all works as it should.

Thanks for looking

0
$dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=UTF8", $dbUser, $dbPass);

add the charset to the pdo connexion !

MA-Lembarki
  • 19
  • 1
  • 7