Connecting Unity to a DB (read and write) [resolved]

got scripts? Need scripts?
Post Reply
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Connecting Unity to a DB (read and write) [resolved]

Post by Callan S. »

Okay, so weve had a thread on this before and this is the code I got (which I still don't fully understand) for reading the database.

Code: Select all

function Awake () {

getname();   

}


function getname()
{
var formText = new Array(); //this field is where the messages sent by PHP script will be in
var inctext;

var URL = "http://localhost/driftwurld/zzzgetdbdata.php"; //change for your URL

      var w = WWW(URL); //here we create a var called 'w' and we sync with our URL and the form
   yield w; //we wait for the form to check the PHP file, so our game dont just hang
   if (w.error != null) {
      print(w.error); //if there is an error, tell us
   } else {
     
     inctext = w.text;
     formText = inctext.Split(":"[0]);
////////////create variables from DB
     GameObject.Find ("Headsup").guiText.text=""+formText[0];
	 

	 
	 //Debug.Log(formText);
    
    
     w.Dispose(); //clear our form in game
   }
}
I was wondering if anyone could give a code example for writing to the database, thus completing the other half of the circle? It'd be much appreciated by many! It'd be cool to have even a check that happens every three or five seconds, and make a semi turn based persistant unity game from it!
Last edited by Callan S. on Tue Jun 12, 2012 2:26 am, edited 2 times in total.
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
xcalpro
Posts: 65
Joined: Sat May 19, 2012 10:25 pm

Re: Connecting Unity to a Data Base (both read and write)

Post by xcalpro »

You basically have a function here that calls a PHP page on the server called zzzgetdbdata.php which does all your db interaction. The results of that page is stored in the array FormText. This array is then parsed and displayed in you HeadUp GUI.

The PHP page is the workhorse which does all the backed stuff like open the db, do the query and send back the results. That's the part your missing and it's the biggest part.
Skills: HTML5, JavaScript, PHP, SQL, Python, BASIC, HeroEngine(HSL), AGS(Lua), Unity, Photon, 3D Max, Mudbox, Photoshop, Poser, Flash
"Jack-of-all-Trades, Master of none"
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Connecting Unity to a Data Base (both read and write)

Post by Callan S. »

I basically can do that part already. It's the matter of writing to the database rather than just reading, that I'm after. Unless the above code can do that somehow?
User avatar
xcalpro
Posts: 65
Joined: Sat May 19, 2012 10:25 pm

Re: Connecting Unity to a Data Base (both read and write)

Post by xcalpro »

You would write to the database from a PHP file the same way you read with the difference being in the SQL Query would UPDATE data into the database instead of a SELECT query.

In this case you may want to create a separate PHP page called zzzputdbdata.php and then pass the data array to the PHP with a POST call. Then the PHP would do an SQL UPDATE to save the data to the db.
Skills: HTML5, JavaScript, PHP, SQL, Python, BASIC, HeroEngine(HSL), AGS(Lua), Unity, Photon, 3D Max, Mudbox, Photoshop, Poser, Flash
"Jack-of-all-Trades, Master of none"
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Connecting Unity to a Data Base (both read and write)

Post by Callan S. »

Yeah, but that's the thing. I look at that code and have no idea how to pass anything via a POST call? Otherwise I totally get you that the page would take the POST and do the write.

So how do I get unity to pass anything by POST?
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Connecting Unity to a Data Base (both read and write)

Post by Callan S. »

Okay, so I'm looking at it again and I realise I'm not clicking I have a command line to play with. So I can try out passing a GET value through and using this code

Code: Select all

function Awake () {

writedata();   

}


function writedata()

{
var URL = "http://localhost/driftwurld/zzzwritedbdata.php?powerfactor=69"; //change for your URL
var w = WWW(URL);

yield w;
if (w.error != null) print(w.error); //if there is an error, tell us
}
And some PHP code to recieve it (and after debugging that - no error messages when I missed a bracket in the PHP!) it took the value and updated the database! So I'm pretty excited about that! (Though I'm wondering how often it runs the code? Guess I could run tests, unless someone already knows?)

But in terms of POST, I'm not sure how I'd use that? Is that better in terms of security? Thanks for your time so far! :)
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
xcalpro
Posts: 65
Joined: Sat May 19, 2012 10:25 pm

Re: Connecting Unity to a Data Base (both read and write)

Post by xcalpro »

Skills: HTML5, JavaScript, PHP, SQL, Python, BASIC, HeroEngine(HSL), AGS(Lua), Unity, Photon, 3D Max, Mudbox, Photoshop, Poser, Flash
"Jack-of-all-Trades, Master of none"
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Connecting Unity to a Data Base (both read and write)

Post by Callan S. »

EDIT: Okay, messed around with my code and found that actually WAMP does not like me naming a file with a hash in its name. Perhaps it's anti drugs ;) So the post method works, thanks for the help xcalpro! I'll leave the code below and for anyone else, just don't name your PHP files with a hash anywhere in the title! Once I've tested this a bit more, I'll edit a [resolved] into the title of this thread. I'll try and make a tute at some point as well, I think!

Code: Select all

function Start () {
writedata();   
}

function writedata()
{
var URL = "http://localhost/driftwurld/zzzwritedbdata2.php"; //change for your URL

var form = new WWWForm();
form.AddField("email","String to pass through!");

var w = WWW(URL,form);

yield w;

if (w.error != null) print(w.error); //if there is an error, tell us
else print("Passed form through");
}
and the php script (this time I'm trying to change the email field)

Code: Select all

<?php
include_once 'connect.php';

if (isset($_POST['email']))
{
$email=$_POST['email'];
$updateplayer="update players set email='$email' where id=10 limit 1";
mysql_query($updateplayer) or die("Could not update player");
}
?>
Post Reply

Return to “Scripting/Coding”