Mysql fetch with if statment

hi i am thinking doing a select statment with while fetch row what i need is something that will do select all the rows bt the column “display” have value 0 display with an echo OFF and the ones with value 1 display all the data with echo ON

so i thought something like

while($row = mysql_fetch_array($result)) if $row[“display”] == 0 echo OFF else $row[“display”] ==1 echo off {

expected result

michael 06/07/85 Chigago ON
rick 63/30/36 new york OFF

is that possible if so how can i???

Yes you can, infact you have practically done it. :slight_smile:

Here’s the code.


while($row = mysql_fetch_array($result)) {

  if(isset($row['display']) && $row['display'] == 0) {

    echo "Do something here";

  }else{

    echo "Do something else here";

  }

}

The ‘isset’ in the if statement checks to see if the varible has something set to it.

if i have this while loop wher would i put the if statment

$query = "SELECT * FROM driversnew"; 
$result = mysql_query($query); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
 // yes so print them one after another 
  echo "<table cellpadding=10 border=1>"; 

  while($row = mysql_fetch_array($result))  { 
     echo "<tr>"; 
     echo "<td>".$row["id"]."</td>"; 
     echo '<td><img class="picture" src="images/' . $row['image'] . ' " width="35"  height="40 " /></td> ';
     echo "<td>".$row["name"]."</td>"; 
     echo "<td>".$row["location"]."</td>";
     echo "<td>".$row["date_of_birth"]."</td>";
     echo "<td>".$row["car_number"]."</td>";
     echo "<td>".$row["favourite_track"]."</td>";
     echo "<td>".$row["least_favourite_track"]."</td>";
     echo "<td>".$row["achievements"]."</td>";
     echo "<td>".$row["sponsors"]."</td>";
     echo "<td>".$row["email"]."</td>";
     echo "<td><a href=deletedrivers.php?drivers=".$row["id"].">Delete</a></td>"; 
     echo "<td><a href=modifydriversform.php?id=".$row["id"].">Modify</a></td>";
     echo "<td><a href='uploadtestform.php'>Add Row</a></td>";
     echo "</tr>"; 
   } //End While
   echo "</table>"; 
} //End if rows 

Just put the if inside the loop where you want ‘OFF’/‘ON’ to be displayed. And put ON and OFF between quotes, they are strings you want to display.