Jump to content

MYSQL_FETCH if statement troubles


Michael4172

Recommended Posts

I'm doing a simple IF test statement below. I would expect it to return the $rows if $key_row equals anything other than a blank, and would expect it to return keyword wasn't found if its blank. However, when I use the code below it returns keywork and definition if true, but returns a blank page if they weren't found. Am I overlooking something below?

[code]
while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if ($key_row != ""){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }
[/code]
Link to comment
https://forums.phpfreaks.com/topic/12984-mysql_fetch-if-statement-troubles/
Share on other sites

[code]

while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if ($key_row > 0){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }

[/code]


[code]

while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if (! $key_row){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }

[/code]


[code]

while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if ($key_row == true){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }

[/code]
Try this

[code]

while($row = mysql_fetch_array($result))
  
     $key_row = $row['keyword'];
  if ($key_row != ""){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";
  } else {
      echo "The keyword wasn't found";
  }
[/code]
[!--quoteo(post=388339:date=Jun 26 2006, 11:37 PM:name=phpstuck)--][div class=\'quotetop\']QUOTE(phpstuck @ Jun 26 2006, 11:37 PM) [snapback]388339[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Try this

[code]

while($row = mysql_fetch_array($result))
  
     $key_row = $row['keyword'];
  if ($key_row != ""){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";
  } else {
      echo "The keyword wasn't found";
  }
[/code]
[/quote]

This code works correctly if the keyword isn't in the DB...but displays the blank page if it does exist....kinda odd...

[code]

while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if ($key_row > 0){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }

[/code]

I tried this, It gives a "Keyword wasn't found" when I type in a keyword that's in the database. It returns a blank page when an incorrect keyword is entered.

[code]

while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if (! $key_row){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }

[/code]

This does same thing as #1
[code]

while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if ($key_row == true){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else {
      echo "The keyword wasn't found";}
  }

[/code]

This returns the keyword when its found. But returns a blank page when the keyword isn't found. :(

In addition I've also attempted = true and === true on this one as well. Also tried $key_row = 1...


I also have tried this

[code]
while($row = mysql_fetch_array($result))
  {
     $key_row = $row['keyword'];
  if ($key_row == true){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";}
  else if ($key_row == false){
      echo "The keyword wasn't found";}
  }
[/code]

It returns the keywords if true, but returns a blank page if false :(
the reason why ^ returns blank page is because you do this:

$key_row = $row['keyword'];

before your if statement. this makes it true, even if there is no value. you are creating the variable, so simply doing if ($key_row == true) ... well it exists, even if it is empty, so it's true.

what is your query string? are you expecting more than 1 row returned? something like this should work if you are expecting more than 1 row returned. there are more efficient ways if only 1 row is expected to be returned

[code]
$found = FALSE;
while($row = mysql_fetch_array($result)) {
   //not sure how your data is stored, so this should cover your bases
   if (($row['keyword'] != NULL) && (!empty($row['keyword'])) && (trim($row['keyword']) != "")){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";
      $found = TRUE;  
  }
}

if ($found == FALSE) {
   echo "not found";
}
[/code]
[!--quoteo(post=388351:date=Jun 27 2006, 12:16 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 27 2006, 12:16 AM) [snapback]388351[/snapback][/div][div class=\'quotemain\'][!--quotec--]
the reason why ^ returns blank page is because you do this:

$key_row = $row['keyword'];

before your if statement. this makes it true, even if there is no value. you are creating the variable, so simply doing if ($key_row == true) ... well it exists, even if it is empty, so it's true.

what is your query string? are you expecting more than 1 row returned? something like this should work if you are expecting more than 1 row returned. there are more efficient ways if only 1 row is expected to be returned

[code]
$found = FALSE;
while($row = mysql_fetch_array($result)) {
   //not sure how your data is stored, so this should cover your bases
   if (($row['keyword'] != NULL) && (!empty($row['keyword'])) && (trim($row['keyword']) != "")){
      echo $row['keyword'];
      echo "<br />";
      echo $row['definition'];
      echo "<br />";
      $found = TRUE;  
  }
}

if ($found == FALSE) {
   echo "not found";
}
[/code]
[/quote]

I am just expecting one match per keyword, but the code you supplied works great :) Thanks for that.
if you know for 100% certain that you will only get either 1 return or no return, you can simplify that code a bit:
[code]
$num = mysql_num_rows($result);
if ($num > 0) {
   $row = mysql_fetch_array($result);
   echo $row['keyword'];
   echo "<br />";
   echo $row['definition'];
   echo "<br />";
} else {
   echo "none found";
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.