0

i'm not pro in php. i have a simple table like this:

game_id      name      score

and i used this query to get the rank of a player in mysql:

SELECT FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score ASC ) FROM highscores ) ) AS 'rank' FROM highscores where game_id = $game_id

it works and shows the rank in mysql but i cant get the rank number as a parameter in my php code.

I have tried with the following methods:

mysql_fetch_assoc();
mysql_free_result();
mysql_fetch_row();

But I didn't succeed to display (get) the actual value.

user3084088
  • 43
  • 1
  • 6
  • 3
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Jan 11 '18 at 15:37
  • 2
    show the complete code you have – Grumpy Jan 11 '18 at 15:37
  • @Grumpy not the complete code. OP should show a [MCVE] – Jens Jan 11 '18 at 15:38
  • What is the Problem with your query, you get wron data or en error? – Jens Jan 11 '18 at 15:39

2 Answers2

0

As has been notes in the comments you shouldn't use the mysql_* functions. I tend to use mysqli.

You can create a connection

$con = mysqli_connect(serverip,"myname","mypassword","mydatabase");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

And execute a query

$result = $con->query($myquery) or die($con->error.__LINE__);

Once you have successfully executed the query

you can get a row with

$row =  $result->fetch_assoc()

and get the individual fields with

$row['score']
Jaydee
  • 4,138
  • 1
  • 19
  • 20
0
include ('dbconnection.php');


$sql = "SELECT game_id, score, ... from YOURTABLE where game_id = '$game_id'";

now you have to query it like

$query = mssql_query($sql, $connection);

and now u can fetch it with :

 while($row = mssql_fetch_assoc($query))
{
 echo $row["game_id"];
 echo $row["score"];
}

Ced.Str
  • 1
  • 4
  • not works.i think its because the rank field is not a actual field of that row.its a temporary field attached to the row to show an extra information (see the sql query) – user3084088 Jan 11 '18 at 16:07