Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mehrdad995 · Feb 03, 2018 at 01:04 PM · wwwphpwwwformsqlbinary

seperate binary retrieved from sql server

Hello,
I've finally managed to send my custom binary data to my server as an SQL BLOB.
i've used a form which contains 'ID'(as string) , 'gamesave_backup'(as binary) , 'achievements_backup'(as binary)
the server place them in their positions correctly,
but whenever I want to retrive them back, the server sends both binaries as one bigger binary file.
everything works as expected. the only problem is that how should I separate two binary data from each other? should I split that in unity just like strings? or maybe should I use two differend php codes to send back binary datas individually?

 $uniqueID = $_POST["uniqueID"];
 
 $server = 'localhost';
 $username = 'username';
 $password = 'password';
 $databaseName = 'database';
 $tableName = 'table';
 
 $select   = "SELECT * FROM $tableName WHERE uniqueID='$uniqueID'";
 
 $db = mysql_connect($server,$username,$password)or die('could not connect to database!: ' .mysql_error());
 $result = mysql_select_db($databaseName) or die('Could not select database');
 $result.mysql_query($select,$db);
 
 while ($row = mysql_fetch_array($result))
     echo $row['uniqueID']   . ":"
         . $row['save_bkp'] . ":"
         . $row['achievements_bkp']  . "\n";
 
 
 // Close the connection, we're done here.

 mysql_close($connection);

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 03, 2018 at 01:59 PM

You currently mix binary data with text. However binary data can represent any possible characters between 0 and 255. You send back a concatenated string where you seperated your three parts using a colon character. However this is dangerous since your binary data could contain a colon (decimal 58, hex: 0x3A). So you don't have a chance to actually split your "text" according to your used seperator.


Generally there are several techniques possible.

First you can encode your binary data in a human readable format such as base64. This however will increase the number of bytes that need to be transferred. The base64 representation is 1/3rd longer than the original data. So a 300 byte chunk of data would become 400 bytes.


Another way is to not use "text" but to encode all your data in a binary format. For this you would need to send the size of each of your "parts" as a 4 byte integer for example. To encode the length as a 4 byte integer you may use the pack function in PHP with the format string "L". In Unity you can use the BinaryReader to actually read the data. Keep in mind that you have to read the data in the exact same order that you write that data. So of course the length information has to come before the actual data. This also might have issues with different byte orders between systems so it might not be the best solution.


Another way would be to still interpret the result as "text" but use an escape character in your binary data. A common escape character would be the backslash "\". However that means you have to pre-process your binary data and replace all occurrences of backslashes and your seperation characers (colon and newline) with an "escaped version". So a backslash in the data need to be replaced by "\\" and a colon by something like "\c" and a newline character by "\\n". When reading the data in C# you can savely split the string at the newline and colon characters since the binary data doesn't contain any of those anymore. However you have to "unescape" the data on the C# side. So you simply search through the data and replace any "\c" with ":", any "\\n" with "\n" and any "\\" with "\".


Since an HTTP request already has quite a bit of overhead for relatively small amounts of binary data you can just use a base64 encoding. Of course it should also be possible to simply encode all your data in a JSON string. JSON actually uses backslash escaping inside string values. So any json parser should do all the unescaping work for you. PHP has the json_encode function to convert an array data structure to json. I'm not really familiar with PHP but i think you could actually pack all your "$row"s into an array and convert the result to json. This should give you something like

 [
     {"uniqueID" : "YOUR_ID", "save_bkp" : "BINARY_DATA", "achievements_bkp" : "BINARY_DATA"},
     {"uniqueID" : "YOUR_ID", "save_bkp" : "BINARY_DATA", "achievements_bkp" : "BINARY_DATA"},
     {"uniqueID" : "YOUR_ID", "save_bkp" : "BINARY_DATA", "achievements_bkp" : "BINARY_DATA"}
 ]

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mehrdad995 · Feb 03, 2018 at 05:27 PM 0
Share

thanks a lot,
it was very informative.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

78 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW Class is acting up 1 Answer

WWWForm.Addfield not able to post data to server 1 Answer

Why isn't this $_POST working properly? 1 Answer

Uploading custom filetype to MySQL database 1 Answer

wwwform send an array the right way! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges