Re: Checking multiple grades
PHP Code:
<?php
$user = $_SESSION['user_id'];
$permissions = array(252, 254, 255);
$loginquery = mssql_query("SELECT UGradeID FROM Account WHERE UserID = '$user'");
$ugradeid = mssql_result($loginquery, 0, 'UGradeID');
$_SESSION['UGradeID'] = $ugradeid;
if (in_array($_SESSION['UGradeID'], $permissions))
{
alertbox('You have no permissions to access here.', 'ragezone.com');
die();
}
?>
Re: Checking multiple grades
You can also use an array in combination with the in_array function. Saves you some lines.
Re: Checking multiple grades
Don't do it this way. Use the fact that the SQL engine will do this for you:
Code:
SELECT COUNT(*) FROM Account WHERE UserID = '$user' AND UGradeID IN ($gradelist)
Additionally, if you're going to refer to "grades" by IDs, which I assume is a FK into another table, you're creating a dependence of your static code on data, which is dubious. Either fully generalize it or just carve out specific roles, and if those are finite (and they should be, like Guest, Member, Moderator, Admin), this query becomes a lot simpler.
Re: Checking multiple grades
I really do wonder though.
Is there a reason why nobody ever uses LIMIT 1 when they only need 1 row replied ?
Did something in MySQL change ? or what ? i really hardly see posts with limit 1 included even if its a sample..
Edit:
Ohhh! got 1,100 posts!
Missed my 1000 mark.. so yay! its my missed 1000 mark lol.
Re: Checking multiple grades
Quote:
Originally Posted by
ThuGie
I really do wonder though.
Is there a reason why nobody ever uses LIMIT 1 when they only need 1 row replied ?
Did something in MySQL change ? or what ? i really hardly see posts with limit 1 included even if its a sample..
It's still up2date and common, but most people skip LIMIT 1, since they know their query will return only one. When I was using PHP/MySQL I was always using LIMIT 1 when expecting only 1 data-set to prevent errors when having more than one result. In practice, this should never happen, ie. you should prevent anybody can register an username that is already taken. ;)
Re: Checking multiple grades
Quote:
Originally Posted by
Nathandj
PHP Code:
<?php
$user = $_SESSION['user_id'];
$permissions = array(252, 254, 255);
$loginquery = mssql_query("SELECT UGradeID FROM Account WHERE UserID = '$user'");
$ugradeid = mssql_result($loginquery, 0, 'UGradeID');
$_SESSION['UGradeID'] = $ugradeid;
if (in_array($_SESSION['UGradeID'], $permissions))
{
alertbox('You have no permissions to access here.', 'ragezone.com');
die();
}
?>
Still dont work, everytime i access i receive " you have no permissions to access here "
Re: Checking multiple grades
Quote:
Originally Posted by
Wertex
Still dont work, everytime i access i receive " you have no permissions to access here "
change in_array to !in_array.
Re: Checking multiple grades
PHP Code:
<?php
/////////////////////////////////////////////////////////////////////////
session_start();//<-- dont forget because u are accessing session data
//////////////////////////////////////////////////////////////////////////
class myClass{
public function __construct(){
mssql_connect("host","user","pass") or die("sql error");
mssql_select("dbName");
}
public function getData($a){
$sql="SELECT UGradeID FROM Account WHERE UserID='{$a}'";
$result = mssql_query($sql);
$row = mssql_fetch_array();
return $row['UgradeID'];
}
}
$userID=$_SESSION['user_id'];
$myObject = new myClass;
switch($myObject->getData($userID)){
case "255": //assuring int or what ever datatype :)quote can be remove
//admin true;
break;
case "254": //assuring int or what ever datatype :)quote can be remove
//gm true;
break;
case "252": //assuring int or what ever datatype :) quote can be remove
//dev true
break;
default:
echo "not permitted";
break;
}
?>
<?php
user id is the index key right? where the GradeID will be fetch?
so now lets first get the usergrade of this user id .. now after getting the data
we will compare it to your condition i prepare use switch statement.
though there are many approach on how to accomplish this program but i prefer switch easy as heaven..
now u forgot to declare session_start()
now causing your query to fail or return to null or error or what ever that return false
Re: Checking multiple grades
Well its more in the case,
If you don't define a limit, mysql will still search the database for more results even though you know there is only 1.
Not sure if it does this if its set to unique..
Re: Checking multiple grades
Quote:
Originally Posted by
ThuGie
I really do wonder though.
Is there a reason why nobody ever uses LIMIT 1 when they only need 1 row replied ?
Did something in MySQL change ? or what ? i really hardly see posts with limit 1 included even if its a sample..
Edit:
Ohhh! got 1,100 posts!
Missed my 1000 mark.. so yay! its my missed 1000 mark lol.
I agree with this 100%!
Re: Checking multiple grades
Quote:
Originally Posted by
Dave
change in_array to !in_array.
thanks dave! is there a way to allow specific userid to join ? since he is co-owner i want him to acces the owner panel, but his rank is 254 is there a way to do like, allow the username of that user in the code?
Re: Checking multiple grades
Quote:
Originally Posted by
Wertex
thanks dave! is there a way to allow specific userid to join ? since he is co-owner i want him to acces the owner panel, but his rank is 254 is there a way to do like, allow the username of that user in the code?
PHP Code:
<?php
$user = $_SESSION['user_id'];
$permissions = array(252, 254, 255);
$allowedUsers = array(1);
$loginquery = mssql_query("SELECT UGradeID FROM Account WHERE UserID = '$user' LIMIT 1");
$ugradeid = mssql_result($loginquery, 0, 'UGradeID');
$_SESSION['UGradeID'] = $ugradeid;
if (!in_array($_SESSION['UGradeID'], $permissions))
{
if (!in_array($_SESSION['user_id'], $allowedUsers))
{
alertbox('You have no permissions to access here.', 'ragezone.com');
die();
}
}
?>
change the array from $allowedUsers with the exception ID's you want to use. Also added the LIMIT 1 as ThuGie said.
Re: Checking multiple grades
Quote:
Originally Posted by
VVess
PHP Code:
<?php
$user = $_SESSION['user_id'];
$permissions = array(252, 254, 255);
$allowedUsers = array(1);
$loginquery = mssql_query("SELECT UGradeID FROM Account WHERE UserID = '$user' LIMIT 1");
$ugradeid = mssql_result($loginquery, 0, 'UGradeID');
$_SESSION['UGradeID'] = $ugradeid;
if (!in_array($_SESSION['UGradeID'], $permissions))
{
if (!in_array($_SESSION['user_id'], $allowedUsers))
{
alertbox('You have no permissions to access here.', 'ragezone.com');
die();
}
}
?>
change the array from $allowedUsers with the exception ID's you want to use. Also added the LIMIT 1 as ThuGie said.
Unfortunately MSSQL does not support LIMIT. You'll have to use TOP(1) after SELECT.
However, I think it's useless since the query will always return 0 or 1 result.
Re: Checking multiple grades
Quote:
Originally Posted by
Dave
Unfortunately MSSQL does not support LIMIT. You'll have to use TOP(1) after SELECT.
However, I think it's useless since the query will always return 0 or 1 result.
I have no experience with MSSQL whatsoever, thank you. I think this will be the correct code then:
PHP Code:
<?php
$user = $_SESSION['user_id'];
$permissions = array(252, 254, 255);
$allowedUsers = array(1);
$loginquery = mssql_query("SELECT UGradeID FROM Account WHERE UserID = '$user'");
$ugradeid = mssql_result($loginquery, 0, 'UGradeID');
$_SESSION['UGradeID'] = $ugradeid;
if (!in_array($_SESSION['UGradeID'], $permissions))
{
if (!in_array($_SESSION['user_id'], $allowedUsers))
{
alertbox('You have no permissions to access here.', 'ragezone.com');
die();
}
}
?>
Re: Checking multiple grades
if (!in_array($_SESSION['UGradeID'], $permissions)) { true statement} <- what if this condition return false what will happen? just die the script u should put some to avoid bugs. ur script is to hard to understand. why u code it in this way. i respect it ofcourse. but i just criticizing it :D