- Home /
Accessing a database though $_GET
After reading the following posts I have a few questions: How can I send and receive data to and from a URL, i.e. server side scripts, web services, etc?
Is it possible to send and recieve data from/to a MySql server?
I guess this is more of a PHP question so I still hope it applies here.
I was wondering if anyone knew of any examples of a php server side script that can accept unity GET request, extract out the parameters and then inset those parameters into a function (this function I will use to query my database)
From what I understand from the UNITY side, I can pass the URL + Parameters through the WWW class to initiate the GET request.
var url = "http://example.com/script.php?var1=value1&var2=value2";
WWW www = new WWW(url);
///And So on
So the question is how i set up my PHP script to be able to do something with this request.
I think I can start out with the following in my php script
<?php
if (isset($_GET['url']))
{
$url = $_GET['url'];
}
?>
Am I understanding this correctly that this will set $url to http://example.com/script.php?var1=value2&var2=value2 ?
If this is correct I should then be able to parse out my parameters into an array that I can feed into my query function. Using a series of string manipulators.
Once I have this array I should be able to feed them into my function:
<?php
function Query(value1, value2)
{
//do some awesome Query stuff
return result;
}
?>
Assuming I have everything right up until now, this is where I get stuck, How to I take the result of my function so that it get sent back to the www result of my unity script?
Answer by DaveA · Aug 15, 2012 at 06:55 PM
For your example:
var url = "http://example.com/script.php?var1=value1&var2=value2";
WWW www = new WWW(url);
Script might be:
<?php
if (isset($_GET['var1']))
$var1 = $_GET['var1'];
if (isset($_GET['var2']))
$var2 = $_GET['var2'];
echo ("got $var1 and $var2");
?>
and your www object will get it in .text like
var result = www.text; // result should now be 'got value1 and value2'
so the www.text will be whatever I echo to the page?
So after your code I can do something like
function Query($var1)
{
//do some stuff to find query result
echo Query result
}
and then www.text will contain the query result?
Yes. And if you are returning stuff formatted as X$$anonymous$$L you can send that into an X$$anonymous$$LDocument and it will parse that for you.
Works! and it was much easier then I thought, Thanks!
Your answer
Follow this Question
Related Questions
Database and Unity 0 Answers
Why i keep getting this message : Wrong response ? 0 Answers
mystery MYSQL error .. 2 Answers
Upload an image onto the database from Unity 1 Answer
www php send data to mysql 2 Answers