Page 1 of 1

If id found do... Else...

Posted: Tue Jun 17, 2008 9:03 am
by jmansa
I'm tearing my head of... I can't find the error...

I have a db table with alot of records. They all have a unique id (sponsor_id) but uid can be there several of times and so can the hole, but only once can the uid and hole be example uid=25 hole=1. Now what I want is to look through the script and look for a certain combination of uid and hole, and if it exists print out "ID FOUND". If it doesnt exist this combo it should go to the next line "NO ID FOUND". Everytime I run this script it shows "ID FOUND" even though the combo doesnt exists... Where do I go wrong?

Code: Select all

$result = mysql_query("SELECT * FROM sponsor WHERE uid='".$uid."' AND hole='".$hole."'");
while($row = mysql_fetch_array($result))
 
  {
  $id = $row['sponsor_id'];
  }
  
if($id == $id) {
    echo 'ID found: '.$id.''; }
elseif($id == 0) {
    echo 'No ID found';
}

Re: If id found do... Else...

Posted: Tue Jun 17, 2008 9:55 am
by WebbieDave
The comparison:

Code: Select all

($id == $id)
will always evaluate to true. I'm not exactly sure what you wish to compare here. If you wish to determine if the current row has a sponser_id that is greater than 0, you can try:

Code: Select all

if ($id) {
 

Re: If id found do... Else...

Posted: Tue Jun 17, 2008 9:58 am
by Frozenlight777
Echo what's in the loop and see what you get... for example

Code: Select all

while($row = mysql_fetch_array($result))
     {
     $id = $row['sponsor_id'];
     echo $id;
     }
also, if you're going to have multiple results and want to check if the id = something... do the if condition inside of the loop...

Code: Select all

while($row = mysql_fetch_array($result))
     {
     $id = $row['sponsor_id'];
     if($id == $id) {
           echo 'ID found: '.$id.'; 
           echo '<br/>';
           }
      else {
                  echo 'nothing';
             }
     }

Re: If id found do... Else...

Posted: Tue Jun 17, 2008 10:01 am
by Frozenlight777
but yeah, $id == $id is always going to be true... so there no validation there... you need to rethink how you want to compare them.

Re: If id found do... Else...

Posted: Tue Jun 17, 2008 11:06 am
by Johnlbuk
You can simply just use the following peice of code.
All it does is decide if the $id is anything other than 0. If the $id is not 0 then echo the $id. If on the other hand $id equals 0 then echo no id found

Code: Select all

if($id!="0") {
    echo 'ID found: '.$id.''; 
}  else {
    echo 'No ID found';
}
John

Re: If id found do... Else...

Posted: Wed Jun 18, 2008 12:21 am
by califdon
You just posted this question yesterday and I gave you an answer, to which you didn't respond. If you want more advice, fine, but don't open up a new thread, just continue posting in the old one.

This topic is locked.