简体   繁体   中英

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 if/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)
}

不像代码那样在第一次迭代后断开,而是在if语句之前的循环中记录这三个值,然后在循环之后测试它们。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM