简体   繁体   中英

How do I change the database values using PHP and MySQL?

I have user table having fields gender and first name like this.. The value stored in the database for gender type is 0 for M(Male) and 1 for F(Female).
I am retrieving the details from database.

By using the following query and displaying the details in table using below code:

My problem is how do I display the 'M' in gender type column if the value is coming from user table for gender is '0'.
And same for Female is I want to display the 'F' if the gender type column if the value is coming from user table for gender is '1'.

Can anyone help on this one?

      modified code :


               GOT an error :Parse error: syntax error, unexpected $end 


               <?php
              $rows=array();
            $query = "SELECT CONCAT(usrFirstname,'',usrSurname) As FullName,usrNickname AS    Nickname,";
            $query.= "usrEmail As EmailAddress,usrGender AS Gender,DATE_FORMAT(usrDOB,'%d%m%y') As DOB,usrBelt AS BeltId";
   $query.= " FROM user";
   $result = mysql_query($query);
 echo mysql_error();
      if($result)
       {
        while($row=mysql_fetch_assoc($result))
  {
       $rows[] = $row;
     }
   }
      <?php 
       if ($row['Gender'] == '0'){
        $Gender = 'M';
           } else {
         $Gender = 'F';

        ?>

     <link href="../../css/styles.css" rel="stylesheet" type="text/css" />
      <div class="subheader" style="margin:16px 0 0;width:980px font-style:bold"><div        class="subheaderl"></div><div class="subheaderr"></div>Users registered at your     facility</div>
       <div class="div" style="overflow-y:scroll;height:500px">
          <table name="t" id ="t" height= "140" width="800">





     <tr style="text-align:left; line-height=10px; word-spacing:0px">
         <thead style="font-weight:bold; font-size:12px;">  
          <th>Full Name</th>
          <th>NickName</th>
             <th>Email Address</th>
              <th>Gender</th>
           <th>DOB</th>
             <th>Belt ID</th>      
       </thead>  
         <?php foreach ($rows as $row){?>
            <tr style="font-size:small">
               <td><?php echo $row['FullName']?></td>
              <td><?php echo $row['Nickname']?></td>
    <td><?php echo $row['EmailAddress']?></td>
    <td><?php echo $Gender?></td>       
    <td><?php echo $row['DOB']?></td>
    <td><?php echo $row['BeltId']?></td>
  </tr>
 <?php }?>

would any one help on this

SELECT CONCAT(usrFirstname,'',usrSurname) As FullName, usrNickname AS Nickname, usrEmail As EmailAddress, CASE WHEN usrGender = 0 THEN 'M' ELSE 'F' END AS Gender, DATE_FORMAT(usrDOB,'%d%m%y') As DOB,usrBelt AS BeltId FROM user

if you put this in the query it return M or F

Add this line, right before you echo out the gender:

$row['Gender'] = $row['Gender'] == 0 ? 'M' : 'F';

Or use macwadu's excellent solution, on editing the query.

or you can do it on php-side:

<td><?php echo ($row['Gender'] ? 'F' : 'M') ?></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM