0

I'm using this HTML to send user input to my database:

<textarea name="description" id="description" cols="30" rows="15"><?php echo $description; ?></textarea>

I parse it with $description = $_POST['description']; and insert it into my Mysql database with INSERT INTO

When I look at the record in my database description is this: “This is one of those books that you don’t want to put down …†Romantic Times

It should be: “This is one of those books that you don’t want to put down …” Romantic Times

QUESTION: how can I save smart quotes and ellipses (...) to my database without them being converted to gibberish?

SOLVED: The solution I found that worked for me was to change my html page to UTF-8 everywhere, so the record would be displayed properly on the page. The record still contains the weird characters though. Here's the html code: meta http-equiv="Content-Type" content="text/html; charset=utf-8 everywhere"

I also used ini_set('default_charset', 'utf-8'); in my PHP page, and now the database has proper text with now weird characters.

sao
  • 1,835
  • 6
  • 21
  • 40
user2816376
  • 127
  • 6
  • 3
    They are not gibberish, [they are in UTF-8](http://www.fileformat.info/info/unicode/char/201c/index.htm). Just handle the character encoding properly (either use UTF-8 everywhere or convert your input text to the encoding you use). – Andrew Moore Oct 11 '13 at 17:22
  • There is no option for UTF-8 everywhere in Starfield Technologies Mysql...at least, not that I can see. Is there a way to set it to UTF-8 Everywhere, inside my php webpage? I'm using utf8_general_ci right now. – user2816376 Oct 11 '13 at 17:25
  • possible duplicate http://stackoverflow.com/questions/6087309/can-i-use-iconv-to-convert-multi-byte-smart-quotes-to-extended-ascii-smart-quote, probably iconv() function will rescue you. – sakhunzai Oct 11 '13 at 17:27
  • You can try using `mysqli_set_charset($mysqli, 'utf8');` and/or `$con->set_charset("utf8");` before inserting into DB. My personal favorite is `$con->set_charset("utf8");` – Funk Forty Niner Oct 11 '13 at 17:28
  • 1
    I figured it out. Thanks. I set my webpage character set to UTF-8 everywhere: meta http-equiv="Content-Type" content="text/html; charset=utf-8 everywhere" – user2816376 Oct 11 '13 at 17:29
  • If you are successful at handle uft8 in mysql , pdf generation will be a headache for you with these characters in db,check the link I shared with you – sakhunzai Oct 11 '13 at 17:32

1 Answers1

2

You should use the CHARACTER SET = utf8 for everything:

  • The MySQL database definition.

    CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATION utf8_general_ci;
    
  • The MySQL table definition.

    CREATE TABLE mytable ( ... ) CHARACTER SET = utf8 COLLATION utf8_general_ci;
    
  • The MySQL session.

    SET NAMES utf8;
    
  • Apache virtual host setting.

    AddDefaultCharset utf-8
    
  • The HTML presentation.

    Content-Type: text/html; charset=utf-8
    

More Resources:

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky

Character Sets Suck by Ligaya Turmelle

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828