4 Replies Latest reply on Feb 4, 2011 1:21 AM by Jorge Raimundo

    Problem with getting utf8 results from MySQL

    Othil Level 1

      Hi,

      I have this problem:
      It is a database with  table that looks like this:

      id (int 10) | header  (varchar 20) | content (text) inch table has utf8_bin coding,
      When I connect everything is fine
      , but as I begin to use the Polish characters such as (ŚŁĄÓŻŹĆ) shows me #$%^&* instead  of Polish characters, while testing gets something like that

      "InvocationTargetException:  There was an error while Invoking the operation. Check your inputs  operation or the server code and try Invoking the operation again.

      Reason: An error while  reading response occured sent to the server. Try suitably encoding the  response before sending it. e.g. If a database column  Contains UTF-8 characters then use utf8_encode () to encode its value  before returning it from the operation. "

      method code

      getAllArtykul public  function () (

      $ Stmt = mysqli_prepare  ($ this-> connection, "SELECT * FROM $ this-> tablename"); $  this-> throwExceptionOnError ();
      mysqli_stmt_execute ($  stmt);
      $ This->  throwExceptionOnError ();
      $ Rows = array ();
      mysqli_stmt_bind_result  ($ stmt, $ row-> id, $ row-> header, $ row-> content);
      while (mysqli_stmt_fetch  ($ stmt)) (
      $ Rows [] = $ row;
      $ Row = new stdClass ();
      mysqli_stmt_bind_result  ($ stmt, $ row-> id, $ row-> header, $ row-> content);
      )
      mysqli_stmt_free_result  ($ stmt);
      mysqli_close ($ this->  connection);
      return $ rows;
      )


      I think he wants me to use utf8_encode  before valuse are called, well, I tried:

      mysqli_stmt_bind_result  ($ stmt, $ row-> id, utf8_encode ($ row-> header), utf8_encode ($  row-> content));

       

      //////////////

      mysqli_stmt_bind_result  ($ stmt, $ row-> id, $ row-> header = utf8_encode ($ row->  header), $ row-> content = utf8_encode ($ row-> content));

       

      ///////////

      $ Header = utf8_encode ($  row-> header)

      $ Content = utf8_encode  ($ row-> content);

      mysqli_stmt_bind_result  ($ stmt, $ row-> id, $ header, $ content);

      ////////

      after trying to  utf8_encode, simply does not show me anything that is in the header and  content (id is displayed), and in desperation I tried to use the  utf8_encode on all the variables that are in the method which ended with  an appearance of error, searched google, I found only one interesting reply


      http://forums.adobe.com/message/2656094


      also this solution does  not work and displays of anything,

       

      What is wrong with UTF8 ???  I'm trying to solve it for like 3 hour and nothing,


      Please help, and sorry for my English

        • 1. Re: Problem with getting utf8 results from MySQL
          Deh Bontempos Level 1

          Hey!

           

          have you figured out this utf-8 problem in php?

           

          please let me know. I'm running into this issue and I can't get the other's examples to make my code working..

           

           

          thanks!

          • 2. Re: Problem with getting utf8 results from MySQL
            SunilAdobe Adobe Employee

            If it's already in UTF-8, then no need to convert. Can you use this function to determine what is the charset it is giving and to convert it to UTF-8

             

            function fixEncoding($in_str)

            {

              cur_encoding = mb_detect_encoding($in_str) ;

              if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))

                return $in_str;

              else

                return utf8_encode($in_str);

            }

             

            You can also find more information in the help link of the API mb_detect_encoding

            http://php.net/manual/en/function.mb-detect-encoding.php

             

            If you are not sure whether your db is not UTF-8, the following commands also should help, then you don't need to explicitly encode.

             

            MYSQL - ALTER database_name DEFAULT CHARACTER SET utf8;

            PHP - mysql_query( "SET NAMES utf8", $database_connection );

            PHP - mysql_query( "SET CHARACTER SET utf8", $database_connection );

             

            Thanks

            -Sunil

            • 3. Re: Problem with getting utf8 results from MySQL
              Othil Level 1

              Hey,

               

              Yap problem with UTF-8 is a pain in the ***, I try to solve this problem for

              about 2 days asking on many forums, and finaly someone give me the answer

              here is as php class which return UTF-8 values from mySQL,

               

              <?php

               

              class ArtykulServices{

               

              public function getArtykul(){

               

               

              $connection = mysqli_connect("localhost", "root", "", "dotestowania", 3306)

              or die (mysqli_connect_error());

               

              $sql = "SELECT * FROM artykul";

               

              mysqli_set_charset($connection, "utf8");

               

              $result = mysqli_query($connection, $sql) or die("query faild:

              ".mysqli_error($connection));

               

              $rows = array();

               

              while($row = mysqli_fetch_object($result)){

               

              $rows[] = $row;

               

              }

               

              return $rows;

               

              }

               

              }

               

              ?>

               

               

               

              Good luck

               

              2010/8/3 Deh Bontempos <forums@adobe.com>

               

              Hey!

              >

              have you figured out this utf-8 problem in php?

              >

              please let me know. I'm running into this issue and I can't get the other's

              examples to make my code working..

              >

              >

              thanks!

              >

              • 4. Re: Problem with getting utf8 results from MySQL
                Jorge Raimundo Level 2

                In my case it helped to add the following to the config file of mysql (my.ini in windows):

                 

                character-set-server=utf8
                default-collation=utf8_unicode_ci