- Home /
PHP HighScore Position
Hi,
i would like to find out position of player based on his score via my database. I am succesfuly getting names and scores, tho i dont know how to get players position. As i think i cant send the position in the fetch array. Best would be if i coud send some separate variable. But i dont know how to read separate variables in Unity. It seems to me that i am able to read only one string from one table ?
here is php code:
$database = mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('myscores') or die('Could not select database');
$result = mysql_query('SELECT * FROM `scores` ORDER by `score` DESC LIMIT 10', $database);
while($row = mysql_fetch_array($result))
{
echo $row['name']. " ". $row['score']. " ";
}
I woud like to add something like this :
$playerPosition = mysql_query('SELECT COUNT FROM scores
WHERE score > $score');
echo $playerPosition;
tho, i dont know how to read it in Unity and if thast possible. But it seems to me out of place to place it in the loop as i need only once the position.
Thanks for any help
Luke
Answer by MPink · Aug 12, 2011 at 10:35 AM
1st off the query
$playerPosition = mysql_query('SELECT COUNT FROM scores WHERE score > $score');
is in the wrong type of string. You need to use a string with "" marks to have variables placed into them so your query wont work.
2nd im not sure what it would return.
$playerPosition = mysql_query("SELECT COUNT(score) AS Count FROM scores WHERE score > $score");
$row = mysql_fetch_array($playerPosition);
echo $row['Count'];
That should output the position that $score is in your table but my SQL isnt great.
COUNT is an SQL function but im not sure if its any different in MYSQL.
To read a web page in unity ues the WWW class to make the http request.
Watch out for the crossdomain.xml though.
You can make GET or POST requests using WWW so sending the name and receiving the index should be ok.
$database = mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db('myscores') or die('Could not select database');
$result = mysql_query('SELECT * FROM scores
ORDER by score
DESC LIMIT 10', $database);
$position = 1; while($row = mysql_fetch_array($result)) { if ($row['name']==$_REQUEST['name']) { echo $position; break; } $position++;
}
Iv not tested any code just written it.
This should return the position of the 1st score of the player named 'name' in the top 10 scores where name is passed by GET in the url.
http://localhost/getrank.php?name=my_name
Removing 'LIMIT 10' from the query would return the 1st position of the name. If the name is not found nothing will be returned.
w3schools.com/sql Should contain the info you need to formulate a better SQL query.
Your answer
![](https://koobas.hobune.stream/wayback/20220612175217im_/https://answers.unity.com/themes/thub/images/avi.jpg)