UTF-8 and question marks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Stivenson

    #1

    UTF-8 and question marks

    I'm using PHP 4.3.10 & MySQL 4.1.9.

    When I read UTF-8 string from database and echo it to the page I get
    only question marks. The number of question marks is equal to the real
    length of the string (not doubled as I would expect).

    Where is the problem?

    Thanks in advance.

  • Andy Hassall

    #2
    Re: UTF-8 and question marks

    On 28 Feb 2006 14:50:07 -0800, "John Stivenson" <john_stivenson @yahoo.com>
    wrote:
    [color=blue]
    >I'm using PHP 4.3.10 & MySQL 4.1.9.
    >
    >When I read UTF-8 string from database and echo it to the page I get
    >only question marks. The number of question marks is equal to the real
    >length of the string (not doubled as I would expect).
    >
    >Where is the problem?[/color]

    Have you set the page to be encoded in UTF-8 with a Content-type header (not
    just a <meta> tag)?

    Can you post a URL to the page (which would help in working out what is adding
    the question marks - whether it's corrupted before it goes out, or whether the
    browser is unable to show the characters due to an incorrect character set
    encoding header).

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • John Stivenson

      #3
      Re: UTF-8 and question marks

      I set content type to UTF-8 using META tag. In response header the
      browser gets only "Content-Type: text/html".

      However, I think that the problem isn't in content type. I can type
      UTF-8 encoded text between HTML tags and it is displayed correctly in
      browser. I can also use "echo 'some UTF-8 encoded text';" and all is OK
      again. But if I read a string from database and try to echo it I get
      only ??????.

      I use these lines to read from database:
      $res = mysql_query($qu ery);
      $row = mysql_fetch_ass oc($row);

      Sorry, but I haven't yet uploaded my page to a public web server.

      Comment

      • Chung Leong

        #4
        Re: UTF-8 and question marks


        John Stivenson wrote:[color=blue]
        > I'm using PHP 4.3.10 & MySQL 4.1.9.
        >
        > When I read UTF-8 string from database and echo it to the page I get
        > only question marks. The number of question marks is equal to the real
        > length of the string (not doubled as I would expect).
        >
        > Where is the problem?
        >
        > Thanks in advance.[/color]

        You just sure the Unicode text was correctly inserted into the
        database? You might just be getting a bunch of question marks from the
        MySQL.

        Comment

        • mannerboy

          #5
          Re: UTF-8 and question marks

          $res = mysql_query($qu ery);
          $row = mysql_fetch_ass oc($row); // $res....

          Comment

          • John Stivenson

            #6
            Re: UTF-8 and question marks

            > You just sure the Unicode text was correctly inserted into the[color=blue]
            > database? You might just be getting a bunch of question marks from the MySQL.[/color]

            The text is displayed correctly in phpMyAdmin.

            Comment

            • Chung Leong

              #7
              Re: UTF-8 and question marks

              Hmmm. Do an "echo mysql_client_en coding();" and see what you get.

              Comment

              • John Stivenson

                #8
                Re: UTF-8 and question marks

                > Hmmm. Do an "echo mysql_client_en coding();" and see what you get.

                It returns latin1.

                * * *

                I forgot to mention that I had used phpMyAdmin to enter text into
                database. If I choose an utf8_* collation I can see text correctly
                displayed in phpMyAdmin.
                However, reading it using PHP and printing it produces only question
                marks.

                Today I found a MySQL database which uses UTF-8. Just for test I read a
                string from the database and printed it to the web page. It was
                displayed correctly!!!
                The collation was set to latin1_swedish_ ci, but it seems that this
                affects only sorting order and not data read from the database.
                The same text was unreadable in phpMyAdmin. It displays some strange
                characters (two chars per letter).

                Maybe there is a bug in phpMyAdmin?

                Comment

                • Chung Leong

                  #9
                  Re: UTF-8 and question marks

                  John Stivenson wrote:[color=blue][color=green]
                  > > Hmmm. Do an "echo mysql_client_en coding();" and see what you get.[/color]
                  >
                  > It returns latin1.
                  >
                  > * * *[/color]

                  One thing to try is run the SQL statement "SET NAMES 'utf-8'" before
                  you run any queries.

                  Comment

                  • John Stivenson

                    #10
                    Re: UTF-8 and question marks

                    Thanks, Chung. That's it.

                    I've also read about character set support from MySQL Reference Manual
                    so now I can understand strange behavior I experienced.

                    Comment

                    Working...