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
2
Question by buxton4life · May 05, 2012 at 11:52 PM · wwwmysqlphpscores

Retrieving data from a WWW php call?

Hello people,

Just wondering how you slice up data retrieved from a mysql database and how I can use that information in the game by setting them as variables.

Here's the code

function getAchieve()

{ var createuser_url = "**/resources/get_achieve.php" + "?username=" + user + "&password=" + pass + "≤vel=" + levelTest;

var cu_ get = new WWW(createuser_url);

 yield cu_get;
 if(cu_get.error) {

     print("There was an error checking admin: " + cu_get.error);

 }
 else
 {
     var data = cu_get.text;
     var words = data.Split(","[0]);
     words[0] = starAchieve;
     words[1] = deathAchievement;
     words[2] = ultimate;

     Debug.Log("WBESKSJSJ"+starAchieve);
 }

}

PROBLEM : The problem is in the data.Split as my Debug.Log() shows up when it is place before this piece of code and not after! Any advice would be much appreciated thanks. Or any advice on how to properly split the download data.

BTW - starAchieve, ultimate, deathAchievement are all variables set at top of script.

Comment
Add comment · Show 9
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 ByteSheep · May 06, 2012 at 01:55 AM 2
Share

Looks like the Split function is fine, maybe try checking if data is a valid string using Debug.Log(data); .. Or change the line var data = cu_get.text; - to - var data = "One,Two,Three"; and see if the error still occurs.

avatar image Bunny83 · May 06, 2012 at 02:52 AM 1
Share

What's the point of receiving the data, splitting it up into an array and then overwrite the content of that array? I guess you want it the other way round?! The received data is of course just text so we need to know the type of those variables (starAchieve, deathAchievement, ultimate). Are they booleans, ints or also strings?

Also a sample data string would help to analyse what happens and what should happen.

avatar image buxton4life · May 06, 2012 at 03:13 AM 0
Share

Thanks for the reply guys.

$$anonymous$$erry crimbo I tried the "One, Two, Three" all works perfectly with no errors. So the error must be in the cu_get?? (maybe :/)

Bunny, yes wrong way round (it's been a long night) ha. The var are as such :

private var deathAchievement; private var starAchieve; private var ultimate;

I have not defined their type. These vars will be used for working out achievements, scores etc across the web.

Just tried again and the Debug.Log(); fails to initiate when "One, Two, Three" is replaced with cu_get.text...:S i'm guessing the error is in the format of the cu_get.text.

avatar image Bunny83 · May 06, 2012 at 03:24 AM 0
Share

So the first thing you would try, before asking a question like this, is to print out the content of your data variable to see what you got back from your php script, right? If you have done this, might it be possible to share your knowledge with us? Or what do you expect as answer? We can't test what your php script is returning....

Another thing is why do you use untyped dynamic variables? This could get you in trouble depending on how you use the variables. In your case they are all strings, so comparing them to a number or using them in equatations would fail.

avatar image Bunny83 · May 06, 2012 at 04:44 PM 1
Share

@buxton4life: I'm sorry, i guess it was a bit harsh, but we're facing a lot questions on this site which are quite obvious at the first glance ;)

To answer your last comment: The protocol used by a webserver is http (hypertext transfer protocol). It usually returns plain text (websites html text) but also any arbitrary data (like images, or any other files that can be downloaded). The easiest way to receive data is in text form since text can be processed by most systems.

There is no special way how you should return data to Unity since it depends on your script in which format it expects the data.

Btw: your answer is much appreciated. It's good you figured it out yourself. It's not a master solution for all others, but it's a good example to get others on the right track.

Show more comments

1 Reply

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

Answer by buxton4life · May 06, 2012 at 12:43 PM

Ok boys and girls here is a php call which involves a relational database in mysql. Data is processed into an array then split up and converted to floats.

PHP:

 <?php 
     require 'connection.php';

     $username = $_REQUEST['username'];
     $password = $_REQUEST['password'];
         
 
      $query = "SELECT users.*, level1.* FROM users INNER JOIN level1 ON users.user_id = level1.user_id
       WHERE users.username = '$username' AND users.password = '$password'";

       $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
 
      $num_results = mysql_num_rows($result);

 for($i = 0; $i < $num_results; $i++)
 {
      $row = mysql_fetch_array($result);
 echo $row['star'] . "," . $row['death'] . ",";
      
 }

UNITY SCRIPT -

  private var deathAchievement;
  private var starAchieve;
  private var ultimate;

 function getAchieve()

  {
 var createuser_url = "http://localhost:8888/*******/resources/get_achieve.php" + "?username=" + user + "&password=" +    pass + "&level=" + levelTest;
 var cu_get = new WWW(createuser_url);
 yield cu_get;
 if(cu_get.error) {

     print("There was an error checking admin: " + cu_get.error);

 }
 else
 {
     data = cu_get.text;
     var values : String[] = data.Split(","[0]);        
     var v1 : float = parseInt(values[0]);
     var v2 : float = parseInt(values[1]);
      var v3 : float = parseInt(values[2]);
 }


May be some help to some people!

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 MFKJ · Nov 03, 2016 at 05:59 AM 0
Share

i am unable to retrive data getting empty reply from server. what is the problem? can you help?

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

7 People are following this question.

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

Related Questions

Unity php functions 1 Answer

A node in a childnode? 1 Answer

How to get different variables from PHP page ? (example included)... 1 Answer

Unity CrossDomain.xml Still Required? What is wrong? 0 Answers

Android build, wwwform gives ssl.SSLHandshakeException 1 Answer


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