• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

PHP/MySQL and UTF-8

Howdy,
  I'm using PEAR's DB (using mysqli) class to read/write to the database in my PHP application.  I have the MySQL database tables using the utf8_general_ci collation.  After I create the connection to the database, I send the query: "SET NAMES utf8" to ensure that MySQL knows it's getting UTF-8 character data.  The question I have, is do I need to run all data to be inserted into the database through PHP's utf8_encode() function first?  And if that's the case, is there a way to automate that so I don't forget?

- Sean
0
headzoo
Asked:
headzoo
  • 2
  • 2
1 Solution
 
neviencCommented:
Make pages with uft-8 encoding. Save needed php files with utf-8 encoding and don't forget to specify encoding in head tag - <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">. All content will be in utf-8 and you won't need to convert encoding.
0
 
blue_hunterTechnical ConsultantCommented:
iconv()  <--- use this to convert the string into UTF code, when printing / echo the string from php
feel free to check out some issues with the iconv()
0
 
headzooAuthor Commented:
"Make pages with uft-8 encoding. Save needed php files with utf-8 encoding and don't forget to specify encoding in head tag - <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">. All content will be in utf-8 and you won't need to convert encoding."

That is certainly a possibility.  What I don't like about the solution is Notepad++ and SciTE won't automatically detect the character set in a document if you don't use the BOM.  And the BOM screws up PHP scripts.  So that means I have to remember to set the correct encoding each time I save a script.  And I'm forgetful. :)

"iconv()  <--- use this to convert the string into UTF code, when printing / echo the string from php
feel free to check out some issues with the iconv()"

The biggest issue is making sure all data is encoding in UTF-8 prior to sending it to the database.  Having to use a function like iconv() prior to each database query opens up the possibility for bugs, since I may forget to encode the string first.  So if I'm going to use a function like iconv(), I need to find a way to get PEAR's DB class to encode the string for me, so that I don't have to worry about it.  One possibility is to extend (or modify) the mysqli class, but I honestly don't know which method(s) in the class send all the queries to the database, so I don't know where to make the modifications.
0
 
neviencCommented:
Just downloaded n++ 3.5. If the encoding is utf-8 (without BOM), there is check at Format -> UTF-8 without BOM, but encoding shows as ASNI.
0
 
headzooAuthor Commented:
"Just downloaded n++ 3.5. If the encoding is utf-8 (without BOM), there is check at Format -> UTF-8 without BOM, but encoding shows as ASNI."

Right, I'm using 3.5.  Encoding the page in UTF-8 without the BOM isn't a problem.  It's that Notepad++ can't or doesn't automatically set the correct encoding when you open a document encoded in UTF-8 w/o the BOM.  That means I have to remember to check that little "UTF-8 without BOM" every time I'm saving a document.  And like I said, I'm forgetful.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now