?
Solved

PHP/MySQL and UTF-8

Posted on 2006-04-28
5
Medium Priority
?
481 Views
Last Modified: 2010-08-05
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
Comment
Question by:headzoo
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
nevienc earned 2000 total points
ID: 16568249
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
 
LVL 9

Expert Comment

by:blue_hunter
ID: 16569213
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
 

Author Comment

by:headzoo
ID: 16569241
"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
 
LVL 4

Expert Comment

by:nevienc
ID: 16569304
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
 

Author Comment

by:headzoo
ID: 16569337
"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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question