- Home /
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.
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.
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.
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.
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.
@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.
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!
i am unable to retrive data getting empty reply from server. what is the problem? can you help?