结果与 if / else 语句混淆

发布于 2024-12-12 06:40:31 字数 1279 浏览 0 评论 0原文

这是我到目前为止的一些脚本:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱殇璃 2024-12-19 06:40:32

您需要使用 == 进行比较,而不是 =
更改

if($roles = 1)

to

if($roles == 1)

else if($roles = 2)

to

else if($roles == 2)

如果您使用赋值 (=) 而不是比较 (==),它不仅会计算结果为 true,而且还会更改变量。

You need to use == for comparison instead of =:
Change

if($roles = 1)

to

if($roles == 1)

and

else if($roles = 2)

to

else if($roles == 2)

If you use assignment (=) instead of comparison (==) it will not only evaluate to true, but it also will change the variable.

旧城空念 2024-12-19 06:40:32

您正在设置 $roles 的值,而不是检查相等性。尝试将您的代码更改为:

if($roles == 1) {
  ...
}
else if ($roles == 2) {
  ...
}

You're setting the value of $roles instead of checking for equality. Try changing your code to:

if($roles == 1) {
  ...
}
else if ($roles == 2) {
  ...
}
維他命╮ 2024-12-19 06:40:32

到处都是打字错误:

if($roles = 1) {

应该是

if($roles == 1) {

第一个是在做赋值,所以 if() 是在做赋值。新版本正在进行比较,并且可能评估为 false。

Typos everywhere:

if($roles = 1) {

should be

if($roles == 1) {

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.

深海不蓝 2024-12-19 06:40:32

更改

if($roles = 1) 

if($roles == 1)

相同

else if($roles = 2)

Change

if($roles = 1) 

by

if($roles == 1)

same for

else if($roles = 2)
戏剧牡丹亭 2024-12-19 06:40:32

那么有什么理由不能这样做(没有 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;

//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 $roles;

So is there any reason you can't just do this (no 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;

//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 $roles;
浮世清欢 2024-12-19 06:40:32

嗯......

我不是一个大型 PHP 程序员,但是 if 语句不起作用引起了我的注意:

$roles = ($arr5[0]);
if ($roles = 2) {

没有按照你的想法做。这是一个设定值等号。您所做的是将 $roles 的值设置为 2。

那么,为什么要在 if 语句中设置某些内容呢?

然后,您可以执行如下操作:

if (($roles = $arr5[0]) == 2) {

在这里,我设置 $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:

$roles = ($arr5[0]);
if ($roles = 2) {

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:

if (($roles = $arr5[0]) == 2) {

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.

东京女 2024-12-19 06:40:32

让您知道,您的整个代码完全没有意义。
它必须只有几行,而不是两页。

$us3r = mysql_real_escape_string($us3r);
$sql  = "SELECT * FROM users WHERE username = '$us3r'"; 
$res  = mysql_query($sql); 
$userdata = mysql_fetch_assoc($role); 
// now you have all user's data in the $userdata array.
// echo $userdata['username'];  for example will echo a username
// no need for the separate queries, no need to write the same code twice.

// and, finally
echo $userdata['userrole'];
//it seems the only thing your code does - echoing the actual userrole value:

to let you know, your whole code makes absolutely no sense.
instead of 2 pages it have to be of just a few lines.

$us3r = mysql_real_escape_string($us3r);
$sql  = "SELECT * FROM users WHERE username = '$us3r'"; 
$res  = mysql_query($sql); 
$userdata = mysql_fetch_assoc($role); 
// now you have all user's data in the $userdata array.
// echo $userdata['username'];  for example will echo a username
// no need for the separate queries, no need to write the same code twice.

// and, finally
echo $userdata['userrole'];
//it seems the only thing your code does - echoing the actual userrole value:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文