5 Replies Latest reply on Mar 20, 2010 4:40 PM by Expedition_Cirkus

    Newbie question concerning Flash Builder 4 / php / mysql / encode_utf8

    _boba fett_

      I've been following the "Tutorials for developing Data-centric Application with Flex 4" and could succesfully reproduce the Chapter 5-application (Paging with PHP).

      Now I want to alter this example with my own MySQL-database. But when I set some info in the Auto-detect Return Type (I mean the two integer parameters) I get an error message stating that I have to use encode_utf8.

       

      Can someone help me with this problem? I assume it's not that hard to get it right but I can't find it...

        • 1. Re: Newbie question concerning Flash Builder 4 / php / mysql / encode_utf8
          Radhakrishna Bhat Level 3

          Can you please share your MySQL sample database dump file and the PHP file you are using? There may be some specific database setting that is causing the issue.

           

           

          -Radhakrishna

          • 2. Re: Newbie question concerning Flash Builder 4 / php / mysql / encode_utf8
            _boba fett_ Level 1

            Radhakrishna

             

            When I run my tester-script (PHP) I can see the content of my table.

            Here's my PHP (PagingService.php):

             

            <?php
            /**
            * This sample service contains functions that illustrate typical service operations.
            * This code is for prototyping only.
            *
            * Authenticate users before allowing them to call these methods.
            */
            class PagingService {
                /* Connection variables */
                var $username = "*****";
                var $password = "*****";
                var $server = "localhost";
                var $port = "3306";
                var $databasename = "db_bib";
                var $tablename = "bib";
                var $connection;
               
                public function __construct() {
                    $this->connection = mysqli_connect($this->server, $this->username, $this->password, $this->databasename, $this->port);
                    $this->throwExceptionOnError($this->connection);
                }
               
                public function count() {
                    $stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
                    $this->throwExceptionOnError();
                   
                    mysqli_stmt_execute($stmt);
                    $this->throwExceptionOnError();
                   
                    mysqli_stmt_bind_result($stmt, $rec_count);
                    $this->throwExceptionOnError();
                   
                    mysqli_stmt_fetch($stmt);
                    $this->throwExceptionOnError();

             

                    mysqli_stmt_free_result($stmt);
                    $this->throwExceptionOnError();

             

                    return $rec_count;               
                }
               
                public function getItems_paged($startIndex, $numItems) {
                    $stmt = mysqli_prepare($this->connection, "SELECT bib_ID, bib_titel, bib_ondertitel, bib_uitgiftejaar FROM $this->tablename LIMIT ?, ?");
                    $this->throwExceptionOnError();

             

                    mysqli_bind_param($stmt, 'ii', $startIndex, $numItems);
                    mysqli_stmt_execute($stmt);
                    $this->throwExceptionOnError();

             

                    $rows = array();
                   
                    mysqli_stmt_bind_result($stmt, $row->bib_ID, $row->bib_titel, $row->bib_ondertitel, $row->bib_uitgiftejaar);
                   
                    while (mysqli_stmt_fetch($stmt)) {
                        $rows[] = $row;
                        $row = new stdClass();
                        mysqli_stmt_bind_result($stmt, $row->bib_ID, $row->bib_titel, $row->bib_ondertitel, $row->bib_uitgiftejaar);
                    }
                   
                    mysqli_stmt_free_result($stmt);
                    mysqli_close($this->connection);
                   
                    return $rows;
                }
               
                /**
                 * Utility to throw an exception if an error occurs
                 * while running a mysql command
                 */
                private function throwExceptionOnError($link = null) {
                    if($link == null) {
                        $link = $this->connection;
                    }
                    if(mysqli_error($link)) {
                        $msg = mysqli_errno($link) . ": " . mysqli_error($link);
                        throw new Exception('MySQL Error - ' . $msg);
                    }
                }
            }
            ?>

             

            It seems to me that the script contains no error... but one never knows...

            I've also added a screenshot of the definition of the database:

            Schermafbeelding 2010-03-01 om 11.39.39.png

             

            And a screenshot of the fields-definitions of table bib:

             

            Schermafbeelding 2010-03-01 om 11.42.37.png

             

            Let me know when more info is needed!

             

            With kind regards

            • 3. Re: Newbie question concerning Flash Builder 4 / php / mysql / encode_utf8
              Radhakrishna Bhat Level 3

              Hi,

               

              You have to execute query "set names utf8' in your PHP Class. Please refer the bug http://bugs.adobe.com/jira/browse/FB-24643

               

               

              -Radhakrishna

              • 4. Re: Newbie question concerning Flash Builder 4 / php / mysql / encode_utf8
                Expedition_Cirkus Level 1

                Hi Boba Fett,

                 

                I had the same error. This is how I made it work (my additions/changes marked in bold):

                 

                function utf8_encode_array ($array) {

                     $retArray = Array ();

                     foreach($array as $key => $value) {

                          $retArray[$key] = utf8_encode ($value);

                     }

                     return $retArray;

                }

                 

                  public function getItems_paged($startIndex, $numItems) {
                        $stmt = mysqli_prepare($this->connection, "SELECT bib_ID, bib_titel, bib_ondertitel, bib_uitgiftejaar FROM $this->tablename LIMIT ?, ?");
                        $this->throwExceptionOnError();

                 

                        mysqli_bind_param($stmt, 'ii', $startIndex, $numItems);
                        mysqli_stmt_execute($stmt);
                        $this->throwExceptionOnError();

                 

                        $rows = array();
                       
                        mysqli_stmt_bind_result($stmt, $row->bib_ID, $row->bib_titel, $row->bib_ondertitel, $row->bib_uitgiftejaar);
                       
                        while (mysqli_stmt_fetch($stmt)) {

                            $encodedRow = utf8_encode_array ($row);

                            $rows[] = $encodedRow;

                            $row = new stdClass();
                            mysqli_stmt_bind_result($stmt, $row->bib_ID, $row->bib_titel, $row->bib_ondertitel, $row->bib_uitgiftejaar);
                        }
                       
                        mysqli_stmt_free_result($stmt);
                        mysqli_close($this->connection);
                       
                        return $rows;
                    }

                 

                 

                Of course, you need to change every function in the same way. And if you only should return ONE item, then just use utf8_encode (item).

                 

                Hope this helps.

                 

                Fredrik Borgström

                Teambuilding Stockholm

                • 5. Re: Newbie question concerning Flash Builder 4 / php / mysql / encode_utf8
                  Expedition_Cirkus Level 1

                  Noticed an error in my script in case the column names also contains UTF8 characters (which you might want). Then you have to change the function that I provided earlier to this (change in bold):

                   

                   

                  function utf8_encode_array ($array) {

                       $retArray = Array ();

                       foreach($array as $key => $value) {

                             $retArray[utf8_encode ($key)] = utf8_encode ($value);

                       }

                       return $retArray;

                  }

                   

                  /Fredrik