Reputation: 3838
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 :/
Upvotes: 0
Views: 172
Reputation: 157839
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:
Upvotes: 0
Reputation: 107040
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.
Upvotes: 0
Reputation: 54242
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;
Upvotes: 0
Reputation: 1480
Change
if($roles = 1)
by
if($roles == 1)
same for
else if($roles = 2)
Upvotes: 0
Reputation: 360592
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.
Upvotes: 1
Reputation: 36957
You're setting the value of $roles
instead of checking for equality. Try changing your code to:
if($roles == 1) {
...
}
else if ($roles == 2) {
...
}
Upvotes: 1
Reputation: 31569
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.
Upvotes: 4