结果与 if / else 语句混淆
这是我到目前为止的一些脚本:
$check = ("SELECT username FROM users WHERE username = '$us3r'");
$check2 = ("SELECT password FROM users WHERE username = '$us3r'");
$check3 = ("SELECT userID FROM users WHERE username = '$us3r'");
$check4 = ("SELECT userrole FROM users WHERE username = '$us3r'");
//Role
$role = mysql_query($check4);
$arr5 = mysql_fetch_row($role);
$roles = ($arr5[0]);
echo $roles;
if($roles = 1) {
//Username
$results3 = mysql_query($check);
$arr2 = mysql_fetch_row($results3);
$results4 = ($arr2[0]);
//Password
$results5 = mysql_query($check2);
$arr3 = mysql_fetch_row($results5);
$results6 = ($arr3[0]);
//UID
$id1 = mysql_query($check3);
$arr4 = mysql_fetch_row($id1);
$id = ($arr4[0]);
echo 1;
}
else if($roles = 2) {
//Username
$mresults3 = mysql_query($check);
$marr2 = mysql_fetch_row($mresults3);
$mresults4 = ($marr2[0]);
//Password
$mresults5 = mysql_query($check2);
$marr3 = mysql_fetch_row($mresults5);
$mresults6 = ($arr3[0]);
//UID
$mid1 = mysql_query($check3);
$marr4 = mysql_fetch_row($mid1);
$mid = ($marr4[0]);
echo 2;
};
但是,我的 if / else 有问题,如果由于某种原因,当我使用用户角色为 2 的用户时,回显显示 21,我希望它是 11 或 22 :/
this is some of my script so far:
$check = ("SELECT username FROM users WHERE username = '$us3r'");
$check2 = ("SELECT password FROM users WHERE username = '$us3r'");
$check3 = ("SELECT userID FROM users WHERE username = '$us3r'");
$check4 = ("SELECT userrole FROM users WHERE username = '$us3r'");
//Role
$role = mysql_query($check4);
$arr5 = mysql_fetch_row($role);
$roles = ($arr5[0]);
echo $roles;
if($roles = 1) {
//Username
$results3 = mysql_query($check);
$arr2 = mysql_fetch_row($results3);
$results4 = ($arr2[0]);
//Password
$results5 = mysql_query($check2);
$arr3 = mysql_fetch_row($results5);
$results6 = ($arr3[0]);
//UID
$id1 = mysql_query($check3);
$arr4 = mysql_fetch_row($id1);
$id = ($arr4[0]);
echo 1;
}
else if($roles = 2) {
//Username
$mresults3 = mysql_query($check);
$marr2 = mysql_fetch_row($mresults3);
$mresults4 = ($marr2[0]);
//Password
$mresults5 = mysql_query($check2);
$marr3 = mysql_fetch_row($mresults5);
$mresults6 = ($arr3[0]);
//UID
$mid1 = mysql_query($check3);
$marr4 = mysql_fetch_row($mid1);
$mid = ($marr4[0]);
echo 2;
};
However there is something wrong with my if / else if for some reason the echo shows 21 when I use a user with a userrole of 2, I want it to be either 11 or 22 :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要使用
==
进行比较,而不是=
:更改
to
和
to
如果您使用赋值 (
=
) 而不是比较 (==
),它不仅会计算结果为 true,而且还会更改变量。You need to use
==
for comparison instead of=
:Change
to
and
to
If you use assignment (
=
) instead of comparison (==
) it will not only evaluate to true, but it also will change the variable.您正在设置
$roles
的值,而不是检查相等性。尝试将您的代码更改为:You're setting the value of
$roles
instead of checking for equality. Try changing your code to:到处都是打字错误:
应该是
第一个是在做赋值,所以 if() 是在做赋值。新版本正在进行比较,并且可能评估为 false。
Typos everywhere:
should be
The first one is doing an assignment, so the if() is doing an asignment. The new version is doing a comparison instead, and can potentially evaluate to false.
更改
为
相同
Change
by
same for
那么有什么理由不能这样做(没有
if
/else
)?So is there any reason you can't just do this (no
if
/else
)?嗯......
我不是一个大型 PHP 程序员,但是
if
语句不起作用引起了我的注意:没有按照你的想法做。这是一个设定值等号。您所做的是将
$roles
的值设置为 2。那么,为什么要在
if
语句中设置某些内容呢?然后,您可以执行如下操作:
在这里,我设置
$roles
的值并同时检查该值。在大多数现代编程语言中,您使用双等号来测试相等性:
if ($roles == 2) {
PHP 还有一个三等号,它不仅可以测试相等性,还可以测试相同性。
if ($a === $b) {
$a
和$b
不仅彼此相等,而且类型相同。Hmmm...
I'm not a big PHP programmer, but the
if
statement not working caught my attention:is not doing what you think. That's a set value equals sign. What you're doing is setting the value of
$roles
to 2.So, why would you want to set something in your
if
statement?You can then do things like this:
Here, I'm setting the value of
$roles
and checking the value at the same time.In most modern programming languages, you use a doubled up equals sign to test for equality:
if ($roles == 2) {
PHP also has a triple equal sign which can test not only for equality, but sameness.
if ($a === $b) {
Not only are
$a
and$b
equal to each other, but they're also the same type.让您知道,您的整个代码完全没有意义。
它必须只有几行,而不是两页。
to let you know, your whole code makes absolutely no sense.
instead of 2 pages it have to be of just a few lines.