Re: convert special characters.
Karl DeSaulniers <[email protected]>
| Newsgroups | gmane.comp.php.database |
|---|---|
| Message-ID | <[email protected]> |
On Aug 10, 2012, at 6:21 AM, Toby Hart Dyke wrote: > > >> -----Original Message----- >> From: Karl DeSaulniers [mailto:[email protected]] >> Sent: Friday, August 10, 2012 12:08 AM >> To: [email protected] >> Subject: Re: [PHP-DB] convert special characters. >> >> >> On Aug 9, 2012, at 5:08 PM, Toby Hart Dyke wrote: >> >>> >>> >>>> -----Original Message----- >>>> From: Karl DeSaulniers [mailto:[email protected]] >>>> Sent: Thursday, August 09, 2012 10:19 PM >>>> To: [email protected] >>>> Subject: Re: [PHP-DB] convert special characters. >>>> >>>> >>>> On Aug 9, 2012, at 5:41 AM, Karl DeSaulniers wrote: >>>> >>>>> >>>>> On Aug 9, 2012, at 5:37 AM, Karl DeSaulniers wrote: >>>>> >>>>>> Help! >>>>>> >>>>>> How do I get special characters to convert correctly when >>>>>> inserting >>>>>> and reading from my database. >>>>>> For example. >>>>>> >>>>>> é => é >>>>>> ë => ë >>>>>> >>>>>> I keep getting >>>>>> >>>>>> ë and é >>>>>> >>>>>> using htmlentities, htmlspecialchars, etc... >>>>>> >>>>>> Any pointers? Please! >>>>>> Going bald over here. >>>>>> >>>>>> here is my funcitons >>>>>> >>>>>> function stringToMysqlFormat($original_input) { >>>>>> //for inserting the data with special characters INTO >>>>>> mysql >>>>>> $html_encoded = >>>>>> htmlentities(mysql_real_escape_string($original_input)); >>>>>> return $html_encoded; >>>>>> } >>>>>> function mysqlToHTMLFormat($encoded) { >>>>>> //for displaying the data FROM mysql >>>>>> $html_decoded = >>>>>> html_entity_decode(stripslashes($encoded)); >>>>>> return $html_decoded; >>>>>> } >>>>>> >>>>>> TIA, >>>>>> >>>>>> Karl DeSaulniers >>>>>> Design Drumm >>>>>> http://designdrumm.com >>>>>> >>>>> >>>>> >>>>> using UTF-8 >>>>> >>>>> >>>>> Karl DeSaulniers >>>>> Design Drumm >>>>> http://designdrumm.com >>>>> >>>>> >>>>> -- >>>>> PHP Database Mailing List (http://www.php.net/) To unsubscribe, >>>>> visit: >>>>> http://www.php.net/unsub.php >>>>> >>>> >>>> >>>> No one? >>>> Not even a link I can go look this up myself with? >>>> >>>> I have successfuly converted before, but this time the company is >>>> in >>>> the netherlands and their special characters are doing this. >>>> Also, I have the database set to UTF-8, but when I check the >>>> charset: >>>> >>>> $charset = mysql_client_encoding($con); >>>> >>>> it returns latin1? >>>> >>>> Can someone tell me what I am doing wrong here? >>>> Not looking for someone to do it for me. >>>> Just some help figuring out which way to look. >>>> >>>> Why is htmlentities() converting wrong? >>>> >>>> Hope someone out there can help. >>> >>> You probably aren't setting your database connection to UTF-8. Try >>> doing this before you do anything with the database, after you have >>> connected: >>> >>> mysql_query("SET character_set_results=utf8", $dbcnx); >>> mb_language('uni'); mb_internal_encoding('UTF-8'); >>> mysql_query("SET character_set_results=utf8", $dbcnx); >>> >>> Toby >>> >>> >> >> >> No, I hadn't done that! Ok, this is a good point to start from. >> Do I set mysql_query("SET character_set_results=utf8", $dbcnx); >> twice? > > Ah - the perils of copying and pasting, and not actually checking... > > The short answer is - I don't know, probably not. Also, the "$dbcnx" > is the > database connection you have already set up with mysql_connect(). > > To be honest, you should probably give this a read: > > http://www.php.net/manual/en/mysqlinfo.concepts.charset.php > > If I had the time right now, I would ;-) > > Toby > > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > Well I found a fix, don't know if it is the best one, but it worked. Between setting the database connection and calling the database I put this php code. found this on php.net in one of the user comments. $conn = ... mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn); $db = ... It did the trick, but it still uses the mysql_ and not mysqli_. And it works even if my database is charset is different. like latin1. but the table cell that stored the info had to be set to UTF-8. If any of you pro-phpers see this and know this to be bad for business, please let me know. I don't want to just patch something to have an exposure later to hackers or something. Thank you all for your responses. Best, Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php