如何使用PHP和MySQL更改数据库值? [英] How do I change the database values using PHP and MySQL?

查看:91
本文介绍了如何使用PHP和MySQL更改数据库值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的用户表,其字段的性别和名字是这样的。存储在性别类型数据库中的值为M(男)为0,F为女(女)。1
我是

通过使用以下查询并使用以下代码显示表中的详细信息:



我的问题是如果性别类型列中的'M'如果来自用户表的性别为'0',我该如何显示'M'。

与女性相同的是我想显示'F '如果性别类型列的值是来自用户表的性别是'1'。



任何人都可以帮忙吗?

 修改后的代码:


出现错误:解析错误:语法错误,意外$ end


<?php
$ rows = array();
$ query =SELECT CONCAT(usrFirstname,'',usrSurname)至于FullName,usrNickname AS昵称,;
$ query。=usrEmail作为EmailAddress,usrGender AS Gender,DATE_FORMAT(usrDOB,'%d%m%y')作为DOB,usrBelt AS BeltId;
$ query。=FROM user;
$ result = mysql_query($ query);
echo mysql_error();
if($ result)
{
while($ row = mysql_fetch_assoc($ result))
{
$ rows [] = $ row;

$ b <?php
if($ row ['Gender'] =='0'){
$ Gender ='M';
} else {
$ Gender ='F';

?>

< link href =../../ css / styles.css =stylesheettype =text / css/>
< div class =subheaderstyle =margin:16px 0 0; width:980px font-style:bold>< div class =subheaderl>< / div>< div class =subheaderr>< / div>在您的设施中注册的用户< / div>
< div class =divstyle =overflow-y:scroll; height:500px>
< table name =tid =theight =140width =800>





< tr style =text-align:left; line-height = 10px; word-spacing:0px>
< thead style =font-weight:bold; font-size:12px;>
< th>全名< / th>
< th> NickName< / th>
< th>电子邮件地址< / th>
< th>性别< />
DOB< / 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}?>











会对此有帮助

code> SELECT CONCAT(usrFirstname,'',usrSurname)作为FullName,
usrNickname AS昵称,
usrEmail作为EmailAddress,
CASE当usrGender = 0
THEN
'M'
ELSE
'F'
END AS性别,
DATE_FORMAT(usrDOB,'%d%m% y')作为DOB,usrBelt AS BeltId
FROM用户



如果您在查询中加入了M或F


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

这篇关于如何使用PHP和MySQL更改数据库值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆