Totally Confused - Displaying Incorrect MySQL Result

Hi Guys,

Ok this is part of the code i am using:


// CHECK STATUS OF APPLICATION

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Oops theres an error, our highly trained monkeys have been notified.");

$queryaff = "SELECT Status FROM `affprograms` WHERE `AffID` = '$affid' AND `ProID` = '$programid'";

mysql_query($queryaff);
$resultaff = mysql_query($queryaff);
mysql_close();

if($resultaff="Pending"){
	$affstatus="Pending";
} elseif($resultaff="Approved"){
	$affstatus="Approved";
} elseif($resultaff="Declined"){
	$affstatus="Declined";
} else {
	//do nothing
}
echo "$queryaff <br /> $resultaff <br />";
echo "TEST OUTPUT - $affstatus";
// END CHECK STATUS OF APPLICATION

Here is the output:

SELECT Status FROM affprograms WHERE AffID = ‘1’ AND ProID = ‘2’
Pending
TEST OUTPUT - Pending

Now the problem is the result is not correct, it shouldnt be returning the value Pending and instead it should be returning the value Approved. Now when i run the MySQL Query in PHPMYADMIN i get the correct result:

So why when i run my script is it returning the incorrect value when im using the same SQL as i have in phpmyadmin?

Any help would be great.

Thank you.

Try…


// CHECK STATUS OF APPLICATION 
mysql_connect(localhost,$username,$password); 
mysql_select_db($database) or die( "Oops theres an error, our highly trained monkeys have been notified."); 
$queryaff = "SELECT Status FROM affprograms WHERE AffID = '$affid' AND ProID = '$programid'"; 
mysql_query($queryaff); 
$resultaff = mysql_query($queryaff); 
$row = mysql_fetch_array($resultaff);
if($row['Status'] ="Pending"){ 
    $affstatus="Pending"; 
} elseif($row['Status'] ="Approved"){ 
    $affstatus="Approved"; 
} elseif($row['Status' ="Declined"){ 
    $affstatus="Declined"; 
} else { 
    //do nothing 
} 
echo "$queryaff <br />"; 
echo "TEST OUTPUT - " . $affstatus; 
mysql_close(); 
 
// END CHECK STATUS OF APPLICATION  


Your code contains multiple errors:


mysql_connect(localhost,$username,$password);

should be:

mysql_connect('localhost',$username,$password);

then in this block:

if($resultaff="Pending"){

    $affstatus="Pending";

} elseif($resultaff="Approved"){

    $affstatus="Approved";

} elseif($resultaff="Declined"){

    $affstatus="Declined";

} else {

    //do nothing

}

you are using the = operator (which is assignment), when you should be using == (which is for an equality test).

i.e.:

if($resultaff == "Pending"){

    $affstatus="Pending";

} elseif($resultaff == "Approved"){

    $affstatus="Approved";

} elseif($resultaff == "Declined"){

    $affstatus="Declined";

} else {

    //do nothing

}

Which is kind of a moot point because your if-else clause doesn’t currently do anything useful - you could simply replace the whole thing with:

$affstatus = $resultaff;

Also you are running mysql_query() twice for no reason.

Hi aamonkey,

Thanks for your help, i have done what you suggest and here is the code i am now using:


// CHECK STATUS OF APPLICATION

mysql_connect('localhost',$username,$password);

@mysql_select_db($database) or die( "Oops theres an error, our highly trained monkeys have been notified.");

$queryaff = "SELECT Status FROM `affprograms` WHERE `AffID` = '$affid' AND `ProID` = '$programid'";

mysql_query($queryaff);
$resultaff = mysql_query($queryaff);
$affstatus = $resultaff;  
mysql_close();

echo "$queryaff &lt;br /&gt;";
echo "TEST OUTPUT - $affstatus";
// END CHECK STATUS OF APPLICATION

How ever i am now getting a

Resource id #6
error as the output :frowning:

Any help would be great.

Thanks

Find this:

$resultaff = mysql_query($queryaff); 

Replace with:

$resultaff = mysql_query($queryaff); 
while ($row = mysql_fetch_assoc($resultaff)) {
    echo $row['Status'];
}

I’ve commented it out and now im getting Resource id #5 error :frowning:

$link = mysql_connect('localhost',$username,$password);
@mysql_select_db($database, $link) or die( "Oops theres an error, our highly trained monkeys have been notified.");

$queryaff = "    SELECT Status
                FROM `affprograms`
                WHERE `AffID` = '$affid'
                AND `ProID` = '$programid'
";

$resultaff = mysql_query($queryaff);
$row = mysql_fetch_row($resultaff);
mysql_close($link);

$affstatus = $row[0];

echo "$queryaff &lt;br /&gt;";

echo "TEST OUTPUT - $affstatus";

Thank you aaMonkey you have been a big help and resolved this problem. I’d also like to thank everyone else who have offered help in resolving the problem i had. Thank you.

Now i have another problem :frowning:


&lt;?
if($affstatus == "Pending"){
	$protext="Your application to run this campaign is under review, please allow 48hrs to review your application.";
} elseif($affstatus == "Declined"){
	$protext="Your application to run this campaign has been Declined. Please speak with your affiliate manager.";
} elseif($affstatus == "Approved"){
	//display banners etc..
	$display=eval('echo "'.$string.'";');
	$protext="&lt;table width='741' border='0' cellpadding='2' cellspacing='2'&gt;
  &lt;tr&gt;
    &lt;td bgcolor='#FFFFFF'&gt;&lt;strong&gt;Banner ID:&lt;/strong&gt; &lt;? echo $bannerid; ?&gt;&lt;br /&gt;
      &lt;img src='&lt;? echo $bannerurl; ?&gt;' /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td bgcolor='#FFFFFF'&gt;&lt;textarea name='BannerCode' id='BannerCode' cols='60' rows='5'&gt;&lt;? echo $display; ?&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;";
}
echo $protext;
?&gt;

The output is fine if $affstatus is Pending. If value is Approved then nothing is displayed and if the value is Declined then it outputs the text but it outputs it 3 times for some reason.

Again any help on this would be fantastic.

Thank you very much guys :slight_smile: