Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Acegikmo · Aug 20, 2012 at 11:55 AM · webplayerwwwmysqlphpform

How do I properly send binary data (byte[]) to a MySQL database?

Hello!

I'm having some issues regarding sending data (In my case, a replay) from a Unity web client to a MySQL database. The replays are built of an array of floats. Each frame of the replay takes up three floats, x position, y position and z rotation.

How do I properly save this data to a MySQL database?

I usually send data in this way:

Unity:

 myForm.AddField("fieldname","MyString");
 WWW upload = new WWW("myScript.php",myForm);



php/SQL:

 if(isset($_POST("fieldname"))){
 
     $myString = $_POST("fieldname");
     $query = "INSERT INTO myTable (myColumn) VALUES('$myString')";
 
 }


However, this is limited to string data or other data saved as string (Such as a float with a value of 2.45f being saved as "2.45"). Saving data as a string is simple, but it takes a LOT of space, since I'm saving frames of a replay, so I have to save it in binary form.

You can convert a float to a latin1 encoded string. A float has 32 bits of information, and a char has 8 bits. This means each float can be converted to a string with 4 characters.

Let's say we have a float value of -4.5367

Splitting this into its 4 bytes, we can interpret them as chars:

 BIN        DEC   Char
 ________________________
 11000000 = 192 = À
 
 10010001 = 145 = ‘
 
 00101100 = 44  = ,
 
 10100101 = 165 = ¥


This means we can use "À‘,¥" to represent this float value.

I got the conversion to work, but sending this doesn't seem to work, as the replays are so long that a character that breaks the code appears almost definitely every single time. (Characters like these: [\,'"´$]), breaking the SQL statement or the php code.

I then tried using myForm.AddBinaryField, saving the floats as a byte array, but the php/SQL code for that one seems really confusing. When I ended up with something I thought would work, it didn't.

I would be really happy if you could help me out with this!

Thanks!

Comment
Add comment · Show 2
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 Cowboy_Jow · Aug 01, 2019 at 06:31 AM 0
Share

How do you retrieve it in unity?

avatar image Bunny83 Cowboy_Jow · Aug 01, 2019 at 08:40 AM 0
Share

When you have a question, please ask a question and do not post an answer to a 7 year old, already answered question.


You retrieve it from your server just like any other data or file. You create a php file which returns the content of the blob / data.

4 Replies

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

Answer by Graham-Dunnett · Aug 20, 2012 at 12:02 PM

Sounds like you are using the correct approach, using WWWForm.AddBinaryData. I don't know what problem you are having on the PHP side of things... Perhaps send a byte array to your PHP that contains the bytes 0,1,2,3,4.....ff and check you can decode that on the PHP side. Then try sending the four-byte sequences 00010203, 04050607, 08090a0ab, etc and check you can decode those. Unity Answers is not a PHP site, so try looking at http://stackoverflow.com/questions/10596356/how-to-obtain-the-post-data-in-binary-of-php.

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 Acegikmo · Aug 20, 2012 at 06:45 PM 1
Share

Thanks! I can solve this using $_FILES ins$$anonymous$$d, as the AddBinaryData is sent as a file.

avatar image
1

Answer by Paulius-Liekis · Aug 20, 2012 at 12:01 PM

WWW class constructor accepts postData:byte[] where you could pack all your data, and then just parse it on the server side.

Comment
Add comment · 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
0

Answer by Htss_samy · Aug 01, 2019 at 09:15 AM

This link will help you. http://www.devindia.biz/how-to-post-and-get-json-data-to-a-service-in-unity-2/

Comment
Add comment · 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
0

Answer by rh_galaxy · Aug 06, 2019 at 04:23 PM

I have also done replay saving to mySql. I scaled my floats with 2^18 to get ints that will fit in 32 bit since I never have a float bigger than +-8192.0... (not ideal however if your values span is very large)

Put it into memory with

 System.Array.Copy(BitConverter.GetBytes((int)(vPos.x * (65536 * 4))), 0, data, iOffs + 12, 4);

Retreive from memory with

 vPos.x = BitConverter.ToInt32(i_pMem, iOffset + 12) / (65536 * 4.0f);

Then I converted the byte array (the binary replay data) to/from base64, and that is what is stored in the db.

From db to binary

 byte[] bytes = System.Convert.FromBase64String(www.downloadHandler.text);

From binary to base64

 string base64 = System.Convert.ToBase64String(bytes);
Comment
Add comment · 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

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

14 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

Related Questions

Android build, wwwform gives ssl.SSLHandshakeException 1 Answer

WWW form string data: best way to encode/escape string data? 1 Answer

When i try to Login by Webplayer it doesn't works but in PC yes. 0 Answers

php WWW not working in unity web player 1 Answer

Problem using WWW with PHP and MYSQL 3 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