This topic is locked
[SOLVED]

 Using $_SESSION("UserName") in an SQL query?

8/13/2012 2:25:47 PM
PHPRunner General questions
L
lane author

Hi,
I'm trying to use the $_SESSION("UserName") variable created after login to filter the results of a query. Right now my query looks like this:

SELECT

*

FROM lead_info

WHERE Owner = "{$_SESSION("UserName"}"


This doesn't work, however. I found something about using the WhereAdd() function to accomplish this, but it hasn't worked out for me. Does anybody know what I need to do to get this working?

C
copper21 8/13/2012

What page are you trying to use this on?

C
cgphp 8/13/2012

$_SESSION is an associative array containing session variables available to the current script. Array in PHP use [ not (. Check the manual: http://php.net/manual/en/reserved.variables.session.php

L
lane author 8/13/2012



What page are you trying to use this on?



Sorry, not quite sure what you mean, I'm just trying to use it on a page I created.



$_SESSION is an associative array containing session variables available to the current script. Array in PHP use [ not (.



My query now looks like this:

SELECT

*

FROM lead_info

WHERE Owner = "{$_SESSION['UserName']}"



but it's still not working. The query does work if I hard-code in the name I want to filter by, and

print_r($_SESSION("UserName")

displays the correct value.

C
cgphp 8/13/2012

It's not clear what you are trying to do. The following code is wrong:

print_r($_SESSION("UserName")


Array in php use [ not (.
Do you want to filter the records of the list page? Check the "Before SQL query" event: http://xlinesoft.com/phprunner/docs/before_sql_query.htm

L
lane author 8/17/2012

Thanks for your replies. I did end up finding the solution to my issue. In the "Before SQL Query" event for the page, I added the following line:

$strWhereClause = whereAdd($strWhereClause, "Owner='". $_SESSION["UserName"] ."'");


with "Owner" being the column in the table containing values that might match $_SESSION["UserName"]. Hope this helps anybody else facing the same problem - I know I hate finding threads covering an exact issue I'm facing with the final post containing no information besides "Figured it out, thanks." =)