Where to put the "Else" statement in my code?

here is my code, the SEARCH works fine but when I inserted the else code it displays the following error

B Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\exam1\searchdata2.php on line 102[/B]

my php code:

 <?php
    include"config.php";
   $roll=$_POST['roll'];
   $year=$_POST['year'];
     
	 $sql="select * from `data` where roll='$roll' and year='$year' ";
	  $result=mysql_query($sql);
	 $alvi=mysql_num_rows($result);
if($alvi>0){
?>
     
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr class="table-header">
                <th width="18%"><h4>Roll</h4></th>
              <th width="18%"><h4>Year</h4></th>
              <th width="18%"><h4>Email</h4></th>
              <th width="18%"><h4>Mobile No</h4></th>
			  <th width="18%"><h4>Result</h4></th>
            </tr>
			
			<?php
		while($row = mysql_fetch_array($result))
				{
		?>
		    	        <td><?php echo  $row['roll']; ?> </td>
						<td><?php echo  $row['year']; ?> </td>
						<td><?php echo  $row['email']; ?> </td>
						<td> <?php echo $row['mobile']; ?> </td>
						<td> <?php echo $row['result']; ?> </td>
				     </tr>
					 <?php
						 	  else {
print "Wrong Roll number or Year. Please insert again"
}
?>
						 <?php
						 }
					}
					?>

Where do I put the code now so that it displays an error if it doesn’t find the requested roll number & year!

Right now you’ve put your else in the middle of the while loop.
You can put an else at the end of an if. So, find the closing bracket of your if statement, and add the else right after that.
Some editors highlight the matching closing bracket when you put the cursor next to the opening bracket (and viceversa), that might be of help to you if you are having troubles identifying the right bracket.
And consistent identing of your code might be of great help too.

As mentioned, your problem is you put the else statement in the middle of the while loop. You can’t do that. The if is outside of the while loop, the else corresponding to that if must be outside as well.


if ($alvi>0)
{
    while($row = mysql_fetch_array($result))
    {
         //  While code to execute.
    }
}
else
{
    print ("Wrong Roll number or Year. Please insert again");
}


That’s the basic idea.

DONE THAT, NOTHING HAPPENS!!! HERE’S MY CODE

ERROR MESSAGE:( ! ) Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\php5\result_process.php on line 217

<?php
    include"config.php";
   $roll=$_POST['roll'];
   $year=$_POST['year'];
     
	 $sql="select * from `data` where roll='$roll' and year='$year' ";
	  $result=mysql_query($sql);
	 $alvi=mysql_num_rows($result);
if($alvi>0){
?>
     
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr class="table-header">
                <th width="18%"><h4>Roll</h4></th>
              <th width="18%"><h4>Year</h4></th>
              <th width="18%"><h4>Email</h4></th>
              <th width="18%"><h4>Mobile No</h4></th>
			  <th width="18%"><h4>Result</h4></th>
            </tr>
			
			<?php
		while($row = mysql_fetch_array($result))
				{
		?>
		    	        <td><?php echo  $row['roll']; ?> </td>
						<td><?php echo  $row['year']; ?> </td>
						<td><?php echo  $row['email']; ?> </td>
						<td> <?php echo $row['mobile']; ?> </td>
						<td> <?php echo $row['result']; ?> </td>
				     </tr>
						 
						 <?php
						 }
					}
					?>
					
					</table>
					<?php
	  else <----[B][SIZE=4]Line 217[/SIZE][/B]
			{  
				print ("Wrong Roll number or Year. Please insert again");
			}  
?>

Is there any reason why you have all those opening and closing PHP tags?

Yes, you moved the else out of the while loop. But you didn’t put it right after the if
Like cheesedude says, it has to be like this:


if ($alvi>0)
{
    while($row = mysql_fetch_array($result))
    {
         //  While code to execute.
    }
}
else
{
    print ("Wrong Roll number or Year. Please insert again");
}  

and you have

if ($alvi>0)
{
    while($row = mysql_fetch_array($result))
    {
         //  While code to execute.
    }
}
?>
  </table>    <-- this is causing the error !!!!!!
<?php
else
{
    print ("Wrong Roll number or Year. Please insert again");
}  

You can’t have any code between the if and the else.

um removed the xtra tags! :smiley: It worked finally guys

I was about to mention that – generally I consider it bad form to constantly be opening and closing PHP (or any other server language “scripting” style) that way – but that’s the REAL programming language coder in me talking. As a rule of thumb I consider any php file that has more than one <?php ?> pairing in it to be total rubbish. Same goes for the use of functions without (), the use of double quotes for no reason, etc, etc…

Some sensible indentation wouldn’t hurt either – tabs or spaces, pick one, and LINE THEM UP.

The same also applies to using H4’s inside TH (serious whiskey tango foxtrot), classes to do thead’s job, outdated attributes, inlining width in the markup, using multiple echo statements to do the job of one, etc, etc…


<?php

include('config.php');
$roll=$_POST['roll'];
$year=$_POST['year'];

$sql="select * from data where roll='$roll' and year='$year'";
$result=mysql_query($sql);
$alvi=mysql_num_rows($result);

if ($alvi>0) {

	echo '
	<table class="userTable">
		<thead>
			<tr>
				<th scope="col">Roll</th>
				<th scope="col">Year</th>
				<th scope="col">Email</th>
				<th scope="col">Mobile No</th>
				<th scope="col">Result</th>
			</tr>
		</thead><tbody>';
 
	while ($row=mysql_fetch_array($result)) {
		echo '
			<tr>
				<td>',$row['roll'],'</td>
				<td>',$row['year'],'</td>
				<td>',$row['email'],'</td>
				<td>',$row['mobile'],'</td>
				<td>',$row['result'],'</td>
			</tr>';
	}

	echo '
		</tbody>
	</table>';

} else {

	echo '
	<p>
		Wrong Roll number or Year. Please insert again
	</p>';
	
}

?>

See, if you line up all your indents properly, throw in a few extra blank lines to make each section of code clearer, get rid of all the “markup for nothing” of stuff that should be in your stylesheet, follow the structural rules of using HTML, it’s becomes pretty clear where to put the “} else {” in your code… since it lines up. Even more so in a DECENT editor that has tab-rule lines like notepad2.

… and people wonder why I don’t need/want color syntax highlighting…

By not constantly opening/closing php parsing for no good reason, it’s also less code overall… playing to the simple notion of “The less code you use, the less there is to break.” – something I had drilled into my head along with good code formatting habits by a nun with a metal yardstick two and a half decades ago.

I really think a lot of coders today could use a solid month or two of a yardstick across the knuckles on this stuff.

Off Topic:

Or just hang around here and read your posts for a while. :smiley: I consider you my stick-weilding nun! I do enjoy your posts.