简体   繁体   中英

sign for “all” in where clause, PDO

Is there any sign for selecting all in a where clause? I have a where clause which is based on a variable thats based on the login. My problem is that I want with the same where select all when a certain user is logged in.

the $prefix is dynamic based on user.

("SELECT * FROM tabell WHERE prefix= '$prefix'"); 

Assuming you have the loged user in a session $_SESSION['user'] , try this:

$where = (isset($_SESSION['user']) && $_SESSION['user']=='certain-user') ? '':"WHERE prefix= '$prefix'";
$sql ="SELECT * FROM tabell $where";

So acording to our discussion we had above in the comment section, I try to provide you some ideas on how to solve this problem.

I think one of the best things you could do is creating an array with all the users the exception should apply. Thats why I asked you if there's a way to get all these users from the DB. That would have been perfect. But since it doesn't seem so, I think you have to maintain the array manually.

Something like:

$usersToCheck = array("Username1", "Username2", "Username3", "Username4"); and so on...

Then, when it comes to query the database, do the following:

if (in_array($_SESSION["username"], $usersToCheck)) {
    $query = "SELECT * FROM table1";
}
else {
    $query = "SELECT * FROM table1 WHERE prefix = :prefix";
}

I think thats one of the easiest ways to achieve what you want. If there's a possibility to maintain the users you want to put in the array from the database, you could even generate this array with PHP and you wouldn't have to do it manually.

Solution 2:

You said: "detecting the user is no probs, its done by a session."

I'm not that sure what you mean with that. But if there's something like a $_SESSION["isSpecialUser"] flag, then simply use this one for the if/else condition like:

if($_SESSION["isSpecialUser"] == 1) {
    $query = "SELECT * FROM table1";
}
else {
    $query = "SLEECT * FROM tabl1 WHERE prefix = :prefix";
}

I hope it helped you. If you have any questions, feel free to ask anytime :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM