Link to home
Start Free TrialLog in
Avatar of TecTaoMC
TecTaoMC

asked on

If Else Delima, if query doen's return antying, can't get the else part to dosplay

I'm getting frusted over an if else statememt that won't work.  It is calling one variable from the table.  If the variable is in the table it will display one thing, if the variable is absent, it should display something else.  I've tried !empty, isset, even $num_rows =1, but the content in the else doesn't display.  Here's the code:

Code: [Select]


<?
$result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCode' " )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
  while ($row = mysql_fetch_array($result))
  {
     extract($row);
     
if (!empty( $MyCode  )) {    
echo "here is the code word";
} else {
echo "you need to go back and get the code";
}
$row_count++;
}
mysql_close;
?>


When the code variable is correct the "here is the code word" displays.  when the code variable is incorrect, what should appear as "you need to go back and get the code" is just blank.

Any thoughts on how to structure the query in such a way that eh incorrect text appears?

Thanks,
Avatar of Scott Madeira
Scott Madeira
Flag of United States of America image

It looks like $MyCode will always have a value.  That is what you are passing in to look at.  What you need to look at in your if else statement is one of the fields from your query result or check to see how many rows you get back.

If the codeword is in your database then $num_rows will be greater than 0.  If the codeword is not in the database then $num_rows would be 0.
A couple other things:  your query will only return rows that have the $MyCode codeword in it.  Not sure what you are trying to do with the row_count++ variable.  That should have the same value as $num_roes;


And, if you want to look at the value of the MyCode field from your database rows then you will want to reference $row['MyCode'] and not $MyCode.
Avatar of TecTaoMC
TecTaoMC

ASKER

Thanks for your quick reply.  I read and made some changes but still can't seem to get the variable to whow 0 if no result is returned which fowls up the if else statememt.  The if else have been written a couple of different ways if $row_count == 1 {....}else {....} and if @num_row == 1 {..} If $row_count == 0 {..}

here's the code using
<?php
include("include/session.php");
$MyCodePassed = $_POST['MyCode'];

$result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCodePassed' " )
or die("SELECT Error: ".mysql_error());
$row_count = mysql_num_rows($result);
  while ($row [Mycode]= mysql_fetch_array($result))
  {
     extract($row [Mycode]);
     
if ($row_count == 1) {    
 echo show the page for this}
 if ($row_count == 0) {    
 echo show the page for no result}  
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial