I'm currently working on a PHP project where multiple access rights exists. The logic is simple:  

- When your access level is 2, you can see a list of your own projects only  

- When your access level is 3, you can see ALL projects

So in SQL:  

First one:

```
SELECT project_name FROM projects

```

Second one:

```
SELECT project_name FROM projects WHERE user_id = user_id

```

The problem is that I'm using PDO with prepared statements and such queries are executed multiple times across the script. This is how it looks like:

```
if ($_SESSION['access_level'] == 3) {
    $sql = "SELECT project_name FROM projects";
  } else {
    $sql = "SELECT project_name FROM projects WHERE user_id = ?";
}

$res = $db->prepare($sql);

// Some more PHP 

if ($_SESSION['access_level'] == 3)
      $res->execute();
    else
      $res->execute(array($_SESSION['user_id']));

```

As I'm doing this in multiple parts of the script, it becomes a mess. Is there a better way to do this? Personally I was thinking of a WHERE-clause where every record is selected. That way this would be possible at the start of the script:

```
if ($_SESSION['access_level'] == 3)
      $id = *;
    else
      $id = $_SESSION['user_id'];

```

Now querying is much easier:

```
$res = $db->prepare("SELECT project_name FROM projects WHERE user_id = ?");
$res->execute(array($id));

```

(Now it will get all records when your access level is 3, but only your own when you're only level 2)

This looks like a pretty dump solution imo as I'm not really usng the WHERE clause how it should be used. Also, using \* is just not possible.

What's the best option for this?

Thank you!