Page 1 of 1

how to change a sql querydynamicaly where user id get change

Posted: Mon Jan 10, 2011 11:32 pm
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...

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

Posted: Tue Jan 11, 2011 3:58 am
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.

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

Posted: Tue Jan 11, 2011 5:50 am
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

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

Posted: Tue Jan 11, 2011 6:04 am
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

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

Posted: Tue Jan 11, 2011 6:10 am
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.