tags:

views:

26

answers:

2

I have a site where a user can have multiple permissions (view, edit, delete, create). Each of these is stored as a tinyint(1) field in the database.

I'm looking for the best (quickest?) way to query MySQL to see if the user has the ability to see this page. Here is a snippet:

$user = mysql_real_escape_string($user);
$resultset = mysql_query("SELECT view FROM testbankAccess WHERE uid LIKE $user");
while($result = mysql_fetch_row($resultset)) {
    if($result[0]=='1') { return true; }
}
return false;
A: 
$resultset = mysql_query("SELECT view FROM testbankAccess WHERE uid=$user LIMIT 1");
$result = mysql_fetch_row($resultset)
if($result[0]==1) { return true; }

I suppose it cannot be faster (there's almost nothing happening here) as long as uid is a key in your table.

azram19
+2  A: 

First, it's probably faster comparing uid and $user using = in favor of LIKE.

Then, as you are only interested in the first result, you don't need to loop through them.

$result = mysql_fetch_row($resultset);
return $result[0] == 1;
Johan