Oct
14
2008

MySQL issue with character set

Today i stumbled upon an issue with inserting text into a database, and the character set wasn’t taking it. No mather what i changed in the database, it was inserted wrong time after time.

Characters like â and é are shown wrong. In the variable it is correct, but in database it isn’t.

This is what i had:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

But when i looked into the database it wasn’t correct. Wrong encoding. The tabel was set UTF-8 so that couldn’t be wrong.

This was my solution:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" SET CHARACTER SET utf8 ");
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

Now it was correct inserted, so i could use it properly.

Written by Rene Pot in: PHP Problems, Sugestions | Tags: , , , , ,

3 Comments »

  • Hi Renee,

    could I instert more characters in that string?
    Like the strange ř from Dvořák.
    I am a php dummie, so…

    best, Frank.

    Comment | January 28, 2009
  • Hey,

    yeah you can insert everything you want. The string can be as long as you want.

    Good luck!

    Comment | January 28, 2009
  • Roberto

    Hi Reneee!!!
    Thanks for your nice advice!!! I had that problem and now thanks to you I resolved!!! THANKS THANKS THANKS!!!!

    Comment | July 23, 2009

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes