2 Replies Latest reply on May 31, 2010 7:46 AM by archemedia

    Storing binary data in mySql

    archemedia Level 4



      I want to store user preferences in a database. To avoid creating multiple rows in a table for every user, or to avoid too many fields in a table I want to create an object on the client side, serialize it and then, store this serialized string on the server.


      I would do this as follows:


      var t:Object = {id: 1, color: 0x567834};
      var b:ByteArray = new ByteArray();
      b.position = 0;
      var s:String = b.toString();


      Now I can store s into my database.


      What should I do to reconvert s to an object?


      Help appreciated!



        • 1. Re: Storing binary data in mySql
          David_F57 Level 5



          If you use amfphp you just send the binary straight to the db and retrieve it as binary as well no need for any conversions.


          flex  and remoteobject methods


                  <mx:method name="uploadConfig" result="qryUpLoad(event)" />

                  <mx:method name="downloadConfig" result="qryUserConfige(event)" />

          private function saveConfig() : void


                  var ba: byteArray  = new ByteArray();

                 ........populate ba




          private function getConfig(): void





          private function qryUserConfig(res:ResultEvent): void


          var result: ByteArray = res.result as ByteArray;



          the php


          function uploadconfig($ba) {

          $data = $ba->data;

          $mysql = mysql_connect(localhost,"root","");

          $data = mysql_real_escape_string($data);

          mysql_select_db( "catalog" );

          $Query = "INSERT INTO user (ID,data) VALUES(0,'$data')";

          $Result = mysql_query( $Query );

          return $Result;



          function downloadconfig($id) {

          $mysql = mysql_connect(localhost,"root","");

          mysql_select_db( "catalog" );

          $Result = mysql_query("SELECT data FROM user WHERE ID=$id");

          $row = mysql_fetch_object($Result);


          $config = new ByteArray($row->data);

          return $config;


          • 2. Re: Storing binary data in mySql
            archemedia Level 4

            Thx David, just what I needed!