开发者

If/else statement only working for 1 value of variable

I am using a query called $sqlStr4v that has three results, sorted by a value called totalScore2. Then I am trying to use an if/else statement to display something if a variable called $uv is included in one of the three results returned by the query.

The problem I'm having is that the if/else statement only works when $uv equals a value in the first of the three results. How can I make it work for the other two?

Thanks in advance,

John

The query:

$sqlStr4v = "SELECT 
    ...
ORDER BY totalScore2 DESC 
LIMIT 3";

The i开发者_开发技巧f/else statement:

$resultv = mysql_query($sqlStr4v);

while ($rowv = mysql_fetch_assoc($resultv)) {
  if ($rowv  ['username'] == $uv) {

...

}else 
{ 


...

} 

break;


Why break; if you do not want to break out of the loop?

$resultv = mysql_query($sqlStr4v);

while ($rowv = mysql_fetch_assoc($resultv)) {
  if ($rowv  ['username'] == $uv) {
    echo 'something';
    //break; here if you want to display something at most once.
  }else 
  {
    echo 'nothing';
    //break; here if you want to display nothing at most once.
  }
  //break; here if you only want the first row (result is same as using LIMIT 1)
}


Instead of breaking after the first iteration as your code does, record the three values in a loop before the if statement, then test them after the loop.


The break is causing that. You may take it out of the while loop depending on your requirement. While would execute for every database row so if break is encountered, the loop would terminate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜