Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3128 > unrolled thread

PHP, MySQL and UTF-8?

Started byRobert Latest <boblatest@yahoo.com>
First post2011-09-12 16:46 +0000
Last post2011-09-13 16:53 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  PHP, MySQL and UTF-8? Robert Latest <boblatest@yahoo.com> - 2011-09-12 16:46 +0000
    Re: PHP, MySQL and UTF-8? Jerry Stuckle <jstucklex@attglobal.net> - 2011-09-12 15:30 -0400
    Re: PHP, MySQL and UTF-8? "Peter H. Coffin" <hellsop@ninehells.com> - 2011-09-12 14:31 -0500
      Re: PHP, MySQL and UTF-8? "A" <a@a.a> - 2011-09-12 22:27 +0200
    Re: PHP, MySQL and UTF-8? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-09-13 16:53 +0200

#3128 — PHP, MySQL and UTF-8?

FromRobert Latest <boblatest@yahoo.com>
Date2011-09-12 16:46 +0000
SubjectPHP, MySQL and UTF-8?
Message-ID<9d6ralFrpkU1@mid.uni-berlin.de>
Hello all,

I have UTF-8 characters in my database. They're there, I can see them in
a MySQL dump as well as in phpmyadmin.

When I retrieve this data from the db and output it via print(), all I get
is question marks. These come from the server because I see them also in
wget's output.

In phpmyadmin I see the correct characters in my db. When I insert them
literally in my HTML they come out as expected. When I print() them as string
literals in php, they come out correctly as well.

I think I'm opening the db correctly:

$c = new PDO("mysql:host=$host;dbname=$dbname;port=$port;charset=UTF-8",
            $user, $pass);

So ... how do I retrieve UTF-8 chars from a database?

Thanks

robert

[toc] | [next] | [standalone]


#3132

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-09-12 15:30 -0400
Message-ID<j4lmk6$gl0$1@dont-email.me>
In reply to#3128
On 9/12/2011 12:46 PM, Robert Latest wrote:
> Hello all,
>
> I have UTF-8 characters in my database. They're there, I can see them in
> a MySQL dump as well as in phpmyadmin.
>
> When I retrieve this data from the db and output it via print(), all I get
> is question marks. These come from the server because I see them also in
> wget's output.
>
> In phpmyadmin I see the correct characters in my db. When I insert them
> literally in my HTML they come out as expected. When I print() them as string
> literals in php, they come out correctly as well.
>
> I think I'm opening the db correctly:
>
> $c = new PDO("mysql:host=$host;dbname=$dbname;port=$port;charset=UTF-8",
>              $user, $pass);
>
> So ... how do I retrieve UTF-8 chars from a database?
>
> Thanks
>
> robert

What version of PHP are you using?

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#3133

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2011-09-12 14:31 -0500
Message-ID<slrnj6snh2.bek.hellsop@nibelheim.ninehells.com>
In reply to#3128
On 12 Sep 2011 16:46:13 GMT, Robert Latest wrote:
> Hello all,
>
> I have UTF-8 characters in my database. They're there, I can see them in
> a MySQL dump as well as in phpmyadmin.
>
> When I retrieve this data from the db and output it via print(), all I get
> is question marks. These come from the server because I see them also in
> wget's output.
>
> In phpmyadmin I see the correct characters in my db. When I insert them
> literally in my HTML they come out as expected. When I print() them as string
> literals in php, they come out correctly as well.
>
> I think I'm opening the db correctly:
>
> $c = new PDO("mysql:host=$host;dbname=$dbname;port=$port;charset=UTF-8",
>             $user, $pass);
>
> So ... how do I retrieve UTF-8 chars from a database?

Are you sure that you're retrieving them incorrectly and are not
printing them incorrectly instead? Write a programs to extract some
data that has UTF-8 multibyte characters in it and try printing the
bin2hex() of the string instead. And do it using the shell/CLI command
first so you can rule out the myriad browser/http server potential
problems. If the hex is correct for UTF-8 then you can stop looking at
the database connection as the problem and start looking at your
rendering of the characters. If it isn't, THEN you can start caring
about the database connection.

-- 
The pig is nothing but a giant dish which walks while waiting to be 
served.
	    --Grimod de La Reynière

[toc] | [prev] | [next] | [standalone]


#3134

From"A" <a@a.a>
Date2011-09-12 22:27 +0200
Message-ID<j4lq1g$e8t$1@gregory.bnet.hr>
In reply to#3133
1. Your <head> must have this:

    <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>

2. If you load from template file and then print, your template file must be 
saved in UTF-8 (I'm not sure if BOM must be present, try with or without 
it) - UTF8 BOM is 0xEF 0xBB 0xBF - http://unicode.org/faq/utf_bom.html#bom1

3. Execute these queries before querying database rows (only once is 
needed):

    SET CHARACTER SET utf8;
    SET NAMES utf8;

4. After the two above you should be able to retrieve them properly and 
print them as the should be.

Does that help? 

[toc] | [prev] | [next] | [standalone]


#3142

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-09-13 16:53 +0200
Message-ID<1537384.qVoOGUtdWV@PointedEars.de>
In reply to#3128
Robert Latest wrote:

> I have UTF-8 characters in my database. They're there, I can see them in
> a MySQL dump as well as in phpmyadmin.
> 
> When I retrieve this data from the db and output it via print(), all I get
> is question marks. These come from the server because I see them also in
> wget's output.

As for the wget output, your console might not be UTF-8-compliant, or you 
might not have declared the proper character encoding for the HTTP resource 
and the resource type (e. g., HTML) requires you to escape them then, which 
you also failed to do.

> In phpmyadmin I see the correct characters in my db. When I insert them
> literally in my HTML they come out as expected. When I print() them as
> string literals in php, they come out correctly as well.

This and
 
> I think I'm opening the db correctly:
> 
> $c = new PDO("mysql:host=$host;dbname=$dbname;port=$port;charset=UTF-8",
>             $user, $pass);

suggests that the Content-Type header field of the HTTP response for your 
Web site has the wrong value.  And I think that is a FAQ.
 

PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
 -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web