Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to use php session_user data in postgresql view. How is it possible?

I have created postgre view with session_user variable in db.

CREATE OR REPLACE VIEW test_view AS 
SELECT T.name, TT.age
FROM test1 T INNER JOIN test2 TT ON T.client_id=TT.client_id
WHERE T.user::text=session_user::text;

And I have my PHP code with CodeIgnited framework. I have created session in PHP:

$data = array(
 'client_id' => $row->client_id,
 'user' => $row->user,
 'validated' => true
);
$this->session->set_userdata($data);

And now I try to select that view:

$result = $this->db->get('test_view');

But it is not working. The query returns 0 row.

What I'm doing wrong? (I know that I can make function or just select query, but I have to do this with this way - with view).

share|improve this question
add comment

1 Answer

session_user::text in your view definition holds the Postgres' session user (i.e. the database user you connect with), not the one in the app/php session.

Remove the where part of your definition, and add it as part of the query you run in php — using the app/php session user, of course.

share|improve this answer
 
Ok, thanks. I doubt that it isn't php session user. I'll do just regular view and add condition in php. –  riek 1 hour ago
 
Dude… It isn't the php session user. No ifs, no buts, no room for opinions or what you doubt or not. It just isn't. :-) postgresql.org/docs/current/static/functions-info.html Run select session_user; in psql to see for yourself… you'll be outside of PHP and it will return whatever Postgres user you used to connect to Postgres. –  Denis 1 hour ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.