1
Table: cm_rank
|id|rankpoeng|tittel  |
-----------------------
|1 |        0 |Sivil  |
|2 |      322 |Wannabe|
|3 |     1450 |Hitman |

What I'm trying to do:

$currentXp = 500;

$query = mysql_query("SELECT * FROM cm_rank WHERE $currentXp > rankpoeng LIMIT 1");

while ($rows = mysql_fetch_array($query)):
    $levelTitle = $rows['title'];
    echo $levelTitle;
endwhile;

The $levelTitle variable should only hold the text "Wannabe", since the users has more xp than it requires, but not enought for the next rank.

How may I structure up a query like this?

1
  • So what defines but not enought for the next rank. ? Is there a bench mark ? Commented May 13, 2015 at 8:48

2 Answers 2

1

You should order by rankpoeng column to take the highest valid value.

$query = mysql_query("SELECT * FROM cm_rank WHERE $currentXp > rankpoeng ORDER BY rankpoeng DESC LIMIT 1");
Sign up to request clarification or add additional context in comments.

1 Comment

@Hansen: and note that you don't need while loop when you have LIMIT 1, use directyl $row = mysql_fetch_array($query);
1

You have to order the result:

"SELECT * FROM cm_rank WHERE $currentXp > rankpoeng order by rankpoeng desc LIMIT 1"

But you should not use the deprecated mysql_* API. You should use mysqli_*or PDO with prepared statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.