- Home /
Server Side Highscore "There was an error getting the high score: 404 not found"
I'm following the tutorial found on the wiki on how to create server side high scores. found here Here is my current set up:
addscore.php
<?php
$db = mysql_connect('localhost', 'root', 'wiiwowut') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') 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="99669900"; # 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 values (NULL, '$name', '$score');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>
displayscore.php
<?php
// Send variables for the MySQL database class.
$database = mysql_connect('localhost', 'root', 'wiiwowut') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') 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++)
{
$row = mysql_fetch_array($result);
echo $row['name'] . "\t" . $row['score'] . "\n";
}
?>
hscontroller.js
private var secretKey="99669900"; // Edit this value and make sure it's the same as the one stored on the server
var addScoreUrl="http://localhost/addscore.php?"; //be sure to add a ? to your url
var highscoreUrl="http://localhost/display.php";
function Start() {
getScores();
}
function postScore(name, 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(name + score + secretKey);
var highscore_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.
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() {
gameObject.guiText.text = "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 {
gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
What does 404 mean? is it an error with my database setup? MYSQL setup? did I not name something properly in the code?
I"m using Apache 2.2 webserver and PHP, both which seem to be working fine. Or could there really be an issue here?
EDIT: using a test PHP from the localhost
<?php $database = mysql_connect('localhost', 'root', 'wiiwowut') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
echo ("sucessfully connected, hooray!");
?>
yielded sucessful. This has to be an issue within the unity code.
Answer by IndieScapeGames · Jul 19, 2012 at 04:15 PM
404 simply put means the server cannot find the requested file.
You're hosting on your localhost, which is fine, but based on previous Apache experience, you may need to specify the port. If you're using the default port, I don't know why it might not be forwarding automatically.
var addScoreUrl="http://localhost:80/addscore.php?"; //added default port number
var highscoreUrl="http://localhost:80/display.php"; //added default port number
Huh. Well I tried messing around with that, but to no avail. Could it be anything else?
Answer by FredVicentin · Jul 19, 2012 at 04:15 PM
Probably a Firewall problem or a modem problem, check if you have a problem on port, and test your mySQL// Apache to see if it works, do some score and them see if the value on DB changed, if it don't change, can be a index problem, check if you have an index and if the table value is right, like a float variable, etc, check the names and your modem, something can be blocking it.
disabled firewall, added inbound rule etc. and didn't help at all. Thanks though.
Answer by TehWut · Jul 19, 2012 at 08:52 PM
ಠ_ಠ
well, out of all my obvious solutions, this was the worst one. It should've been
var highscoreUrl="http://localhost/displayscore.php";
instead of
var highscoreUrl="http://localhost/display.php";
Your answer
![](https://koobas.hobune.stream/wayback/20220613074416im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Best way to connect to database for my mobile game? 3 Answers
Microsoft SQL Server error. 0 Answers
Hosted Database for Mobile Game 0 Answers
Web-Client / Dedicated Console Server? 1 Answer
php, sql security 3 Answers