Jump to content


Photo

MYSQL_FETCH if statement troubles


  • Please log in to reply
6 replies to this topic

#1 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 27 June 2006 - 03:12 AM

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?

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";}
  }


#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 27 June 2006 - 03:29 AM


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";}
  }




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";}
  }




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";}
  }


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 phpstuck

phpstuck
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 27 June 2006 - 03:37 AM

Try this


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";
  }


#4 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 27 June 2006 - 04:04 AM

[!--quoteo(post=388339:date=Jun 26 2006, 11:37 PM:name=phpstuck)--][div class=\'quotetop\']QUOTE(phpstuck @ Jun 26 2006, 11:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try this


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";
  }
[/quote]

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


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";}
  }


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.


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";}
  }


This does same thing as #1

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";}
  }


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

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";}
  }

It returns the keywords if true, but returns a blank page if false :(

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 June 2006 - 04:16 AM

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

$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";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Michael4172

Michael4172
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 27 June 2006 - 04:25 AM

[!--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) View Post[/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

$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";
}
[/quote]

I am just expecting one match per keyword, but the code you supplied works great :) Thanks for that.

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 June 2006 - 04:41 AM

if you know for 100% certain that you will only get either 1 return or no return, you can simplify that code a bit:
$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";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users