how to change a sql querydynamicaly where user id get change

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ishakya
Forum Commoner
Posts: 40
Joined: Tue Jan 04, 2011 4:58 am

how to change a sql querydynamicaly where user id get change

Post by ishakya »

Hello everyone,
I want to change a query when the logging user id gets change.
Assume if user 1 logged to the system he can view all the fields of table.
if user 2 logged to the system he can view 2 or 3 or may be 5 fields of the table.but user 2 cannot view all the fields.
I think it gonna be changed at where clause of the query.
There should be one select query.query will be change dynamically when logging user id changes.
I hope all understood my problem.
Please be kind enough to share the knowledge.
Thank you...
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to change a sql querydynamicaly where user id get ch

Post by social_experiment »

How many types of users are there? If you only have 2 types it would be easy to create two seperate queries, each already containing fields that can only be viewed by each type of user.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ishakya
Forum Commoner
Posts: 40
Joined: Tue Jan 04, 2011 4:58 am

Re: how to change a sql querydynamicaly where user id get ch

Post by ishakya »

Thank you for reply,but i need to maintain only one select query.

Code: Select all

SELECT (fields name) where userid='***'
select query can be different when the different users are logged in to the system.
But i think it is difficult to that.But my boss want it to happen.

I got your idea to make different access levels but i'm keep trying to fulfill my bosses idea.
So if u can give me a support.
thanks again for your help... :D
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

Re: how to change a sql querydynamicaly where user id get ch

Post by kalpesh.mahida »

Do one thing prepare two array one for holding fields which user with id 1 can have access and second for user with id 2 can access

Like

Code: Select all

$arr1 = array('fld1','fld2','fld3','fld4','fld5','fld6');
$arr2 = array('fld1','fld3','fld5',fld6');
now you have user id of logged in user, right? you just have to put condition to prepare fields part of your query based on logged in user.

Code: Select all

$fieldStr = '';
if($loggedInUid == 1) {
    // prepare $fieldStr here using $arr1
} else if ($loggedInUid == 2) {
   // prepare $fieldStr here using $arr2
}
Hope this will help you.
Kalpesh Mahida
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to change a sql querydynamicaly where user id get ch

Post by social_experiment »

kalpesh.mahida has a similar idea to what i had in mind, using arrays. Once you have the array you can implode the values into a string and insert it into your query.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply