Jump to content

Trying a simple SQL if and elseif statement...


flyersman

Recommended Posts

Im trying to do a simple... if aff in the users table == 0 print this elseif == 1 print that

 

No matter what, its prints this. Heres my code, please see if im doing anything wrong. Thanks!!

<?

mysql_connect("localhost", "xxxx_xxxxx", "xxxxxxxxx") or die(mysql_error());
mysql_select_db("xxxx_xxxxxxxx") or die(mysql_error());

$query  = "SELECT aff, w9, incentive FROM users";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
?>

 

And then later in the code...

<? if ( $row['aff'] == 0 ) { echo "this"; } elseif ( $row['aff'] == 1 ) { echo "that"; } ?>

 

But when aff is == 1 for the user im using, it still prints this. Anybdy know why?

 

Thanks!

I'm pretty sure that when you pull information out of a database, it comes out as a string, so you need to convert it into an integer or wrap the value that you're comparing it to in quotes (both as R0bb0b showed you). Personally I'd use his 3rd suggestion.

<?
$log="Logout";
if($login_page)
$log="Login";
$filearray=explode("/",$_SERVER['PHP_SELF']);
$file=$filearray[count($filearray)-1];
switch($file)
{
case 'dashboard.php': $file_name="Dashboard";break;
case 'campaigns.php': $file_name="Campaigns";break;
case 'reports.php': $file_name="Reports";break;
case 'tools.php': $file_name="Tools";break;
case 'profile.php': $file_name="Profile";break;
case 'payments.php': $file_name="Payments";break;
case 'support.php': $file_name="Support";break;
case 'terms.php': $file_name="TOS";break;
default:$file_name="";break;

}

?>
<?

mysql_connect("localhost", "xxxx_xxxxx", "xxxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxxxxxxxxxxx") or die(mysql_error());

$query  = "SELECT aff, w9, incentive FROM users";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
?>
<? $aff = (int)$row['aff']; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="DJBrewardsATGMAILDOTCOM">
<meta http-equiv="Keywords" content="DJBrewards,freebie,incentive">
<meta name="Description" content="DJBrewards freebie scipt">
<link rel="icon" href="<?=$admin_ext . $config['image_dir']?>favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="<?=$admin_ext . $config['image_dir']?>favicon.ico" type="image/x-icon" />
<?php /*?><link rel="stylesheet" href="<?=$admin_ext?>data/template/style.css" type="text/css" /><?php */?>

<script language="Javascript" src="<?=$admin_ext?>include/AJAX.js"></script>
<script defer type="text/javascript" src="pngfix.js"></script>
<title>Express Revenue - <?=$file_name?></title>
<!-- this style sheet in main dir, because this file is called by a file in main dir-->

<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<body>
<div id="basic">
<div id="logo">
  <table width="706" border="0">
    <tr>
      <td width="19"> </td>
      <td width="597"> <div align="left"><img src="images/logo.png" width="280" height="36" /></div></td>
      <td><div align="center"><a href="logout.php"><img src="images/logout.png" width="42" height="14" border="0" /></a></div></td>
      </tr>
  </table>
  </div>
  <div id="menu">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="720" height="40">
      <param name="movie" value="images/user_menu.swf" />
      <param name="quality" value="high" />
      <param name="WMODE" value="Transparent" />
      <embed src="images/user_menu.swf" width="720" height="40" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="Transparent"></embed>
    </object>
  </div>
</div>
<div id="stat"><img src="images/stats_box.png" /><div id="data" class="style8"><strong><? if(!($admin_page || $login_page))
					{
					load_status_new();
					}?></strong></div>
</div>
<div id="aff">
  <img src="images/aff_mngr_box.png" width="285" height="110"  />
  <div class="style8" id="aff_data">
    <strong>Your Affiliate Manager: <? if( $aff == 1 )
  echo "value is 1";
else if( $aff == 0 )
  echo "value is 0";
else
  echo "value is other: " . $aff; ?></div>
</div>
<div id="background"><img src="images/body_bg_shad.png" width="768" height="18" /><br />
  
   <table width="729" border="0" align="center" cellpadding="0" cellspacing="0" class="style9">
        <tr>
         
          <td align="left" valign="top"><p class="style8" >
<div id="heading_back" class="style8"><strong> <?=$file_name?></strong></div><br />
<? 
echo '<p align="center" >'.page_description($this_page).'</p>'; //this function is saved in include/function.php
?>

That's your problem then.

 

You need to pass the ID of the user through the URL somehow and then do:

$query = 'SELECT * FROM users WHERE id=' . $id; // $id is the ID being passed through the URL
$result = mysql_query($query);

if(mysql_num_rows($result) == 1)
{
  $row = mysql_fetch_assoc($result);

  $aff = (int)$row['aff'];

  // ...
}

As of now, with your current code, it's probably pulling information from the last user in the database. I bet if you changed the last user's "aff" value, you'd notice a difference.

probably not a problem, but what does this

<?php
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
?>

do without an opening and closing curly brace?  Does it just loop through until the last line so $row = last row, or is it the same as $row = mysql_fetch_array($result, MYSQL_ASSOC)?  I'm guessing that Brandon Jaeger is right, you need an id in the query otherwise you are just getting the last row.

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.