-2

I have the following php code:

<?php




if (!isset($_REQUEST['search'])){

    while(($write=mysql_fetch_array($gamesearched)) != null){
    echo "Found!";
    }else{
    echo "No results";
    }

    }
?>

And it's giving me an error:

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\php\www\Gameplay\backgame.php on line 41

Nanne
  • 64,065
  • 16
  • 119
  • 163
John Lemon
  • 81
  • 1
  • 5
  • 4
    Because that is not valid syntax... `else` can only be paired with `if`. – DCoder Jun 15 '13 at 08:46
  • Edited the topic, please read the code again (?) – John Lemon Jun 15 '13 at 08:55
  • You've still got the `else` paired with the `while`, not with the `if`. – jcsanyi Jun 15 '13 at 08:57
  • So what should I do ? – John Lemon Jun 15 '13 at 09:03
  • Welcome ;). If you find an answer, no need to close this topic. What you can do is "accept" the answer below: press the check on the answer that fixes it. If there isn't one, you might want to write down your own solution and mark that as the correct answer. The goal is to have a clear question-and-answer page here, not a forum-topic to throw away :). Also, I don't see what the other question you asked is about that is different from this one, don't open duplicates! Good luck with your code though :) – Nanne Jun 15 '13 at 09:15

5 Answers5

2

PHP doesn't support else in while statements. You will need to use a sentinel instead.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Where is your if??? You are missing IF

Also pay attention that if/else structure is as follows: if() {} -> else {} You still misplaced else, should be outside of if statement.

Brian
  • 4,958
  • 8
  • 40
  • 56
1

I guess you have misplaced the closing brace. See the possible correct codes below.

if (!isset($_REQUEST['search'])){
  while($write=mysql_fetch_array($gamesearched)){
    if($write != null) {
      echo "Found!";
    }else{
      echo "No results";
    }
  } 
}

or

while(($write=mysql_fetch_array($gamesearched)) != null){
  if (!isset($_REQUEST['search'])){
    echo "Found!";
  } else {
    echo "No results";
  }
}
Nagarjun
  • 2,346
  • 19
  • 28
0

In PHP, a while statement can't have an else clause. You need something external to the while that can tell you if it was executed at least once.

How about something like this?

$total = mysql_num_rows($gamesearched);
if ($total > 0) {
    while (($write=mysql_fetch_array($gamesearched)) !== false) {
        echo "Found!";
    }
} else {
    echo "No results";
}

In this case, I've looked up the total number of rows found before I start, but I could also have started by setting a counter to zero and then incrementing it inside the while loop. That would look something like this:

$total = 0;
while (($write=mysql_fetch_array($gamesearched)) !== false) {
    $total++;
    echo "Found!";
}
if ($total == 0) {
    echo "No results";
}

Note that mysql_fetch_array() returns false if there are no more rows, so I've updated the while condition for you as well.

All that being said, there are good reasons not to use mysql_* functions in new code. See this question for more details, and some better alternatives: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52
-1

1)

if(xxxx){
   //do if
}else{
   //do else
}

2)

if(1):
    echo '123';
else:
    echo '456';
endif;
jw.john
  • 362
  • 4
  • 9