为什么我的if语句没有像我期望的那样工作? [英] Why is my if statement not working the way I expect?

查看:148
本文介绍了为什么我的if语句没有像我期望的那样工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下目标:我使用 SELECT * FROM subject 向我的SQL数据库询问。在这之后,我使用 mysqli_fetch_assoc 请求数组。一直到那一切都很好。现在的问题是,当我尝试在每个循环中修改$ genero的值时,取决于它是1还是0.但是$ genero的值永远不会改变,它总是1并且我确信数组是0和1。任何想法,而$ genero的值不会通过循环改变?

I am trying to achieve the following: I ask my SQL database a query using SELECT * FROM subjects. After doing that I ask for the array using mysqli_fetch_assoc. Until that point all is fine. The problem now is that when I try to modify in each loop the value of $genero depending if it's 1 or 0. But the value of $genero never changes, it's always 1 and I am sure that the array is fetching 0 and 1. Any idea while the values of $genero are not changing through the loop?

while ($subject = mysqli_fetch_assoc($result)) {
if ($subject["sexo"] = 1) { 
    $genero = "<img src='images/hombre.png' />";
} else {
    $genero = "<img src='images/mujer.png' />";
}
    echo $genero;
}


推荐答案

您的比较运算符错误。您正在使用 = 这是分配运算符。在你的例子中,它将永远是真的。您需要使用 == 这是比较运算符。

Your comparison operator is wrong. You're using = which is an assignment operator. In your example it will always be true. You need to use == which is a comparison operator.

if ($subject["sexo"] = 1) { 

应该是

if ($subject["sexo"] == 1) { 

这篇关于为什么我的if语句没有像我期望的那样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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