1

this is the string I have in my MySQL db, (TEXT),

Hello 'muffin'

the output when doing

$query = mysql_query("SELECT * FROM articles");
while ($row=mysql_fetch_array($query)) {
<?php echo $row['text']; ?>
}

is

Hello �muffin�

How do I stop this, I want it to output the quotes normally? :( I have made sure it is not an issue with the font, therefore it must be an issue with MySQL.

Goulash
  • 3,708
  • 6
  • 29
  • 48
  • Wow... that is impressive... `mufifn` turned into `muffin` :-P (boooo you made a _ninja_ edit) – Naftali Aug 01 '12 at 19:06
  • @neal LOL!... I think that the issue could be UTF-8, can you show the table structure? – jcho360 Aug 01 '12 at 19:07
  • I set the collation to utf-8_bin, nothing changed, before it was, latin1_swedish_ci – Goulash Aug 01 '12 at 19:11
  • 2
    @King `SET NAMES utf8` is a query you send from php before you start making any queries. `mysql_set_charset('utf8');` – Esailija Aug 01 '12 at 19:11
  • Need to change table and field collation – Gabriel Santos Aug 01 '12 at 19:12
  • are you sure that these are normal quotes, and not something like [this](http://en.wikipedia.org/wiki/Apostrophe)? also make sure that the encoding header of your html file is set to the encoding actually used when printing the values you get from the db – Andreas Aug 01 '12 at 19:19
  • `mysql_set_charset('utf8');` worked :D tysm – Goulash Aug 01 '12 at 19:28
  • @Esailija, as your comment appears to be the answer here, please write it as an answer and get it accepted. – MvG Aug 01 '12 at 20:12
  • Using `mysql_query` in new applications is a very bad idea. Use `mysqli` or PDO unless you have a very good reason why you can't. – tadman Aug 01 '12 at 21:03
  • `mysql_query` has always worked like a charm for me, I've never used mysqli or PDO? Why exactly is it a bad idea? – Goulash Aug 02 '12 at 06:45

1 Answers1

2

You need:

mysql_set_charset('utf8');

Before running any queries. This makes MySQL interpret the strings you sends as utf-8 and send you utf-8 back. Since your webpage is sending utf-8 headers (You wouldn't see in this context otherwise), it only makes sense.

Here you can find more tips: UTF-8 all the way through

Community
  • 1
  • 1
Esailija
  • 138,174
  • 23
  • 272
  • 326