- Home /
Sending values from Unity to PhP to SQL.
Hey, ive currently been fixing around with databases used for highscores. So far im actually able to get information from SQL > PhP > Unity in real time, but i cant seem to send values from unity to the sql database.
I have 2 PhP scripts and one Unityscript
Addingscores (addscores.php)
<?php
$db = mysql_connect('*******', '******_***', '********') or die('Could not connect: ' . mysql_error());
mysql_select_db("******_***", $db) 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'];
$secretKey="******"; # Change this value to match the value stored in the client javascript below
$real_hash = md5($name . $score . $secretKey);
if($real_hash == $hash) {
// Send variables for the MySQL database class.
$query = "INSERT INTO scores(CORRECT_COL_NAMES) VALUES ('null', 'name','$score')";
$results = mysql_query($query, $connection);
}
?>
There is no errors popping up whatsoever on the website so it has been connected properly.
Displayscores on website(display.php)
<head> Getting Scores </head>
<head2>______________</head2>
<head3><strong>Name, Score</strong></head3>
<?php
// Send variables for the MySQL database class.
$database = mysql_connect('*******', '*******_******', '*******') or die('Could not connect: ' . mysql_error());
mysql_select_db('*******_***') or die('Could not select database');
$query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 5";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
echo "
"; $row = mysql_fetch_array($result); echo $row['name'] . "\t" . $row['score'] . "\n"; } ?> This is the script i use in Unity as my controller
private var secretKey="*******"; // Edit this value and make sure it's the same as the one stored on the server
var addScoreUrl="*********************"; //be sure to add a ? to your url
var highscoreUrl="*********************";
var pname : String = "";
var score : long = 0;
var native_width : float = 1920;
var native_height : float = 1080;
function Start() {
getScores();
}
function postScore(pname, score) {
//This connects to a server side php script that will add the name and score to a MySQL DB.
// Supply it with a string representing the players name and the players score.
var hash=Md5.Md5Sum(pname + score + secretKey);
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(pname) + "&score=" + score + "&hash=" + hash;
// Post the URL to the site and create a download object to get the result.
hs_post = WWW(highscore_url);
yield hs_post; // Wait until the download is done
if(hs_post.error) {
print("There was an error posting the high score: " + hs_post.error);
}
}
// Get the scores from the MySQL DB to display in a GUIText.
function getScores() {
cookiecounter.status = "Loading Scores";
hs_get = WWW(highscoreUrl);
yield hs_get;
if(hs_get.error) {
print("There was an error getting the high score: " + hs_get.error);
} else {
cookiecounter.status = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
function OnGUI () {
var rx : float = Screen.width / native_width;
var ry : float = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
if (GUI.Button(new Rect(900,1000,100,70), "Post Record"))
{
pname = cookiecounter.Username;
score = cookiecounter.tcounter;
postScore(pname, score);
}
if (GUI.Button(new Rect(1000,1000,100,70), "Get Record"))
{
getScores();
}
}
My SQL Query
http://gyazo.com/38e02531659aa6ba0a6e6c54f552b78a
How it looks ingame(ignore number at top, also if you know how to remove the coding then please help) http://gyazo.com/2b990c50e9049ecdaa4ab120f266883a
Display on website
http://gyazo.com/09e58d3babc0ad7e215f4196cec1210b
As you can see im actually getting the scores from the SQL to the PhP script and then to Unity realtime, but i cannot seem to do it the other way around. Blanked out passwords etc.
If you need more info please tell me. Thanks in advance ^^
There's so much information here, it is difficult to see what your issue is and where the relevant code is. $$anonymous$$aybe you shouldn't show the code that is working.
Sure, i was just wondering if some of it might be interfering with eachother.
The main problem is addingscores, using the postscore function in the unity script and the addscores php script.
function postScore(pname, 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.
var hash=$$anonymous$$d5.$$anonymous$$d5Sum(pname + score + secret$$anonymous$$ey);
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(pname) + "&score=" + score + "&hash=" + hash;
// Post the URL to the site and create a download object to get the result.
hs_post = WWW(highscore_url);
yield hs_post; // Wait until the download is done
if(hs_post.error) {
print("There was an error posting the high score: " + hs_post.error);
}
}
<?php
$db = mysql_connect('*******', '******_***', '********') or die('Could not connect: ' . mysql_error());
mysql_select_db("******_***", $db) 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="******"; # 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(CORRECT_COL_NA$$anonymous$$ES) VALUES ('null', 'name','$score')";
$results = mysql_query($query, $connection);
}
?>
These two, if that narrows it down a bit?
Answer by akguldeniz · May 01, 2014 at 06:56 AM
i think you can create xml file with unity engine and read it with php than write to database
Your answer
Follow this Question
Related Questions
Register help 0 Answers
Javascript with php 1 Answer
Server-side generated heightmap 0 Answers
www php send data to mysql 2 Answers