- Home /
I want to make a dynamic cloud database.
So... yeah, i'm looking to store usernames, passwords, health, stats, equipment variables etc.
I would like to use a cloud db... preferably free, for testing purposes.
I need a point in the right direction... It seems like i'm going to need to use Php in anything I use...
The database will use MySQL code, and Unity will use monodevelop code... where does the php code go?
Please... just a point in the right direction
http://answers.unity3d.com/questions/306913/loading-from-disk-saving-to-database.html This may serve some purpose
ok... I stumbled upon this a few days back: http://wiki.unity3d.com/index.php?title=Server_Side_Highscores
I'm hoping to dumb- it down since I haven't used php or X$$anonymous$$L before... Lets see if I can do this, if anyone can comment or answer my question, please do!
On http://www.phpmyad$$anonymous$$.net/home_page/index.php You would add:
CREATE TABLE `scores` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCRE$$anonymous$$ENT PRI$$anonymous$$ARY $$anonymous$$EY,
`name` VARCHAR(15) NOT NULL DEFAULT 'anonymous',
`score` INT(10) UNSIGNED NOT NULL DEFAULT '0'
)
TYPE=$$anonymous$$yISA$$anonymous$$;
and then this would go on my website, which the X$$anonymous$$L later addressed would access:
<?php
$db = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
mysql_select_db('my_database') or die('Could not select database');
// Strings must be escaped to prevent SQL injection attack.
$name = mysql_real_escape_string($_GET['name'], $db);
$score = mysql_real_escape_string($_GET['score'], $db);
$hash = $_GET['hash'];
$secret$$anonymous$$ey="mySecret$$anonymous$$ey"; # Change this value to match the value stored in the client javascript below
$real_hash = md5($name . $score . $secret$$anonymous$$ey);
if($real_hash == $hash) {
// Send variables for the $$anonymous$$ySQL database class.
$query = "insert into scores values (NULL, '$name', '$score');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>
This code would be actually displayed by the webpage that a user could view, when they have inputted their username?
<?php
// Send variables for the $$anonymous$$ySQL database class.
$database = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
mysql_select_db('my_database') or die('Could not select database');
$query = "SELECT * FRO$$anonymous$$ `scores` ORDER by `score` DESC LI$$anonymous$$IT 5";
$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['name'] . "\t" . $row['score'] . "\n";
}
?>
This X$$anonymous$$L is the cross-domain policy, which would be a hidden file on my website running parallel to the addscore.php:
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
and of course, this script would be inside the game, communicating to the X$$anonymous$$L which communicates to the addscore.php which communicates to the SQL script located directly on the database?
using UnityEngine;
using System.Collections;
public class HSController : $$anonymous$$onoBehaviour
{
private string secret$$anonymous$$ey = "mySecret$$anonymous$$ey"; // Edit this value and make sure it's the same as the one stored on the server
public string addScoreURL = "http://localhost/unity_test/addscore.php?"; //be sure to add a ? to your url
public string highscoreURL = "http://localhost/unity_test/display.php";
void Start()
{
StartCoroutine(GetScores());
}
// remember to use StartCoroutine when calling this function!
IEnumerator PostScores(string name, int score)
{
//This connects to a server side php script that will add the name and score to a $$anonymous$$ySQL DB.
// Supply it with a string representing the players name and the players score.
string hash = $$anonymous$$D5Test.$$anonymous$$d5Sum(name + score + secret$$anonymous$$ey);
string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
// Post the URL to the site and create a download object to get the result.
WWW hs_post = new WWW(post_url);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
}
// Get the scores from the $$anonymous$$ySQL DB to display in a GUIText.
// remember to use StartCoroutine when calling this function!
IEnumerator GetScores()
{
gameObject.guiText.text = "Loading Scores";
WWW hs_get = new WWW(highscoreURL);
yield return hs_get;
if (hs_get.error != null)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
}
Answer by thornekey · Jan 09, 2014 at 03:29 AM
assuming youre making an online game. You should get smartfox server for unity games, this will let you access a database easily. then you could just look for a free mysql database place - a google search found this site: http://www.db4free.net/ - not sure if reliable. but yes a smartfox server would put you in the right place to start with for simple access to a mysql database - there are many tutorials and videos to learn to set it up. please let me know if i am wrong on assuming an online game.
I am making an online game... I'm also using photon. I couldn't get smartfox to work for me... I tried for 3 days straight (while unemployed - so I mean 3 DAYS STRAIGHT)... you really think smartfox is the way to go?
everything I've read says unity should be able to access the database by itself.
http://wiki.unity3d.com/index.php?title=Server_Side_Highscores
The above link shows me how to do this, I just can't gather where the PHP goes... I assume it would go on my website, but... that shouldn't be nessecary as the game should directly communicate with the database?
hmm whilst i think about this for a bit. i have two things to say: its weird that smartfox isnt working for you, which version are you using... have you looked up tuts for that ? also, i do understand that unity can access databases, AND your "game" can access the database - i dont think you should access the database directly through the game. make sure, whatever u use.. photon, smartfox, or your own server (which i suggest you try and make just so you have TOTAL control in features (i made my own and it works great)) you get the client to tell the server to access it - its just for security measures. just in case u didnt know :) hope it helps. ill keep pondering about it. whatever you do, dont give up. its sad seeing great ideas go down the toilet just because of a little wall you struggle to climb over.
The goal right now is to get the game running so I can add content and get a player base... Even if its 20 people, that would be more than enough.
This is why I'm trying to use clouds. Atm, the photon cloud works client communications, and I need a way to load stats to all players that connect so the replication bugs stop occurring.
Smartfox had some problems running when installing the server if I remember correctly... This gave me some red flags, as I don't want to host a server on my personal internet connection, or a Database would be simple.
Q:Why wouldn't I want the game to access the DB directly? Would the game access a website, then access the cloud DB? If the website doesn't have any security checks, what purpose would the website server other than to communicate from the game to the database?
You probably want to use a web service that you can call from your game client. Then the web service can handle all the specifics like accessing a database, or whatever else you might want. This serves two main purposes: 1. The security aspects. If the web service is on the same server as the database, then nobody can access the database directly. 2. It hides a lot of the data access code, and makes the game code a lot more simple.
$$anonymous$$now of any free testing web services? (opens bing, yet eagerly awaits a response)
Your answer
![](https://koobas.hobune.stream/wayback/20220613125916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AR assets store in private server 0 Answers
Connection between unity and azure cloud sql database. 1 Answer
Point Cloud in Unity 1 Answer
Cloud stored relational database 1 Answer