我有一个名为account的表。
表:帐户
+---------+---------------------+-----------+
| user_id | email | u_name |
+---------+---------------------+-----------+
| 63 | Vicman@gmai.com | Pross |
| 64 | Pink@gmai.com | Pross |
| 73 | Victor@gmail.com | Victorss |
| 74 | ifeco_man@yahoo.com | Ifecosss |
+---------+---------------------+-----------+
但我想将所有显示u_name更改为" 否"来自我的表,其中u_name =" Pross"在我的页面中这样:
+---------+---------------------+-----------+
| user_id | email | u_name |
+---------+---------------------+-----------+
| 63 | Vicman@gmai.com | No |
| 64 | Pink@gmai.com | No |
| 73 | Victor@gmail.com | Victorss |
| 74 | ifeco_man@yahoo.com | Ifecosss |
+---------+---------------------+-----------+
但我的代码在这里用" No"替换所有行。好像放置我的病情是错误的。这是我的代码 php从数据库中获取数据。
<!--############# TABLE CONTENT ##############-->
<tbody>
<?php
while($row = mysql_fetch_array($s1)) {
# Columns from DB
$db_id = $row['user_id'];
$db_email = $row['email'];
$db_uname = $row['u_name'];
# Condition to handle display of u_name row
if($db_uname == "Pross") {
$db_uname_edit = "No";
}
?>
<tr>
<td><?php echo $db_id ?></td>
<td><?php echo $db_email ?></td>
<td><?php echo $db_uname_edit ?></td>
</tr>
<?php } ?>
</tbody>
<!--##########################################-->
但不需要的输出是:
+---------+---------------------+-----------+
| user_id | email | u_name |
+---------+---------------------+-----------+
| 63 | Vicman@gmai.com | No |
| 64 | Pink@gmai.com | No |
| 73 | Victor@gmail.com | No |
| 74 | ifeco_man@yahoo.com | No |
+---------+---------------------+-----------+
为什么?任何人都可以帮我处理上面的代码吗?感谢
答案 0 :(得分:4)
你应该将$ db_uname_edit的值设置为$ db_uname in else part:
if($db_uname == "Pross") {
$db_uname_edit = "No";
} else {
$db_uname_edit = $db_uname;
}
答案 1 :(得分:1)
# Condition to handle display of u_name row
if($db_uname == "Pross") {
$db_uname_edit = "No";
}
else {
$db_uname_edit = $db_uname;
}
这应该有用。
这是缩短版,你不需要if
条款:
<td><?php echo $db_uname == 'Pross' ? 'No' : $db_uname ?></td>
答案 2 :(得分:0)
改变你的&#34;&#34;声明:
if($db_uname === "Pross") {
$db_uname_edit = "No";
}
比较字符串时,你应该使用&#34; ===&#34;运算符而不是&#34; ==&#34;
答案 3 :(得分:0)
如果条件不匹配,您需要在else
条件中添加if
部分,然后代码转到其他条件
if($db_uname == "Pross") {
$db_uname_edit = "No";
} else {
$db_uname_edit = $db_uname;
}
或更好的用户ternary
运算符以减少代码
$db_uname_edit = $db_uname == "Pross" ? "No" : $db_uname;
你可以在DB
查询中执行此操作,这对性能有益
select user_id, email, IF(u_name = 'Pross', 'No', u_name) AS u_name FROM account