If id found do... Else...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

If id found do... Else...

Post 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';
}
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

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

Post 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) {
 
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

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

Post 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';
             }
     }
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

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

Post 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.
Johnlbuk
Forum Newbie
Posts: 19
Joined: Mon Sep 10, 2007 3:01 pm

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

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
Locked