if ($dbcon) {
$sql = "SELECT * FROM `users` WHERE email = '$email' AND password = '$encrypt_pass'";
$result = mysqli_query($dbcon, $sql);
if (mysqli_num_rows($result) > 0) {
$array = mysqli_fetch_array($result, 1);
if (array_key_exists("firstname", $array) && array_key_exists("lastname", $array) && array_key_exists("id", $array)) {
$_SESSION['f_name'] = $array['firstname'];
$_SESSION['l_name'] = $array['lastname'];
$uid = $array['id'];
echo "Session Data Set";
unset($sql, $result, $array);
} else {
echo "Array keys didn't exist";
}
} else {
echo "No Results returned";
}
$sql = "SELECT permission_level FROM user_permissions WHERE userID = '$uid'";
$result = mysqli_query($dbcon, $sql);
if (mysqli_num_rows($result) > 0) {
$array = mysqli_fetch_array($result, 1);
if (array_key_exists("permission_level", $array)) {
//do what you want with it here
//to get the information you need to use $array['id']
unset($sql, $result, $array);
} else {
echo "Array keys didn't exist";
}
} else {
echo "No Results returned";
}
}
You don't need to do seperate queries for each row in the table. Just use arrays 🙂
PDO is probably beyond you at the moment though, so just sick to this sorta stuff.
I kept it as simple as possible from what you were saying you were looking at doing. I'll run you through it.
- Checks if the database connection is active
- Sets select query to get all rows from users where email = $email and password = $encrypt_pass
- Gets database results
- If statement checks if the if any results were returned, if not, echo's an error
- $array contains the array data
- If statement checks if "firstname, lastname and id" array keys are present (should be if you provided the correct info above)
- sets session variables "f_name, l_name" to "$array['firstname]', $array['lastname'] " respectively
- sets $uid to $array['id'] (should be present cause we checked for it above 😛)
- Error
- Error
- Re-Sets $sql to new query, requesting permission_level from user_permissions where userID = $uid (set above)
- Result
- check for returned results cont.
and repeats itself again.
Whatever you want to do with the "permission_level" value taken from the database on the second query, just remove the // crap from inside the second part of the script and replace it with your script.
Also to access the data, all you need to do is $array['permission_level']
All the echo statements can be removed if you don't want any error msg, add stuff, remove stuff, up to you.
Hope this helps.
Handy Links 😉
PHP Bible
Array Bible