C# Passing string to PHP for a MySQL table : Sever Side Highscore Tutorial

Hello! I follow this tutorial closely and understand the jist of it: Server Side Highscores Tutorial. I got it functioning properly but I wish to make it more dynamic. I managed to pass the name of the MySQL table name for posting scores via the addscore.php script, successfully. This is all so I can use this php scripts for multiple tables, related to different games. I tried doing the same for the display.php but for the life of me I can not get it to work!

In my unity C# script I am only able to get a connection by having my URL contain the variable as well (For some reason concenating the URL will cause a 404 error, i.e. URL + "scoreTable=" + scoretable)

Heres the relative C# code:

public string highscoreURL = "http://website.com/display.php?scoreTable=clicking_game";

    IEnumerator GetScores() {
    gameObject.guiText.text = "Loading Scores";
    WWW hs_get = new WWW(highscoreURL);
    yield return hs_get;
    gameObject.guiText.text = hs_get.text;
}

And the modified display.php:

<?php
// Send variables for the MySQL 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');

$scoreTable = $_GET['scoreTable'];
$query = "SELECT * FROM `$scoreTable` 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'] . "	" . $row['score'] . "

";
}
?>

Just to be extra clear, all I am doing here is changing the ‘scores’ table used in the example to a variable that is passed from unity. This does not work for me however, it says the querey failed because the table does not exist, and the table passed is just ‘’. So connection is being made, and the scoreTable variable is being declared in the PHP script and being used in the querey. But, for some reason my C# code just isn’t passing the string properly. Either that or the PHP script isn’t setting the variable properly.

Any helps or tips would be highly appreciated, thanks!

Rutter! Thank you for your encouraging words of wisdom. I did as you said, and rooted out the broken step. The issue was with the URL I was trying to access. I printed the URL string as I was attempting to connect to it via WWW, and for some reason it was removing the ‘?’ character at the end of the URL, just before the ‘scoreTable=’. Why? I have no clue!! But now that I know that, I was able to concenate URL string with “?scoreTable=” and it worked. So everything I was doing was correct as far as I can see. Something within Unity was qwerking my URL string and holding me back for… far… too… long… Glad I can move on :smiley:

So heads up dudes, sometimes Unity will remove the ‘?’ character at the end of your URL. I can not find the reason for it. Thanks again Rutter :slight_smile: