$_SESSION['username'] in Server-side
$_SESSION['username'] in Server-side

Hi,
just wondering whether there is a way to use $_SESSION['username'] as a string in php serverside in Editor.
$_SESSION['username'] can be inserted into the sql database so I know it's correct.
but when I want to query sql using $_SESSION['username'],
e.g.
"SELECT * FROM mytable
WHERE user = $_SESSION['username']"
Json error occurs.
I also tried to echo $_SESSION['username'] to see whether there's any problem. It seems to be 0 when printed out.
Is there anything about Json data I am missing here?
bear in mind this is in php server side.
Thanks!
This question has an accepted answers - jump to answer
Answers
I think it all goes down to why
function average_number_rating ($editor, $db, $id, $values, $username) {
.............
db->sql("SELECT * FROM mytable
WHERE user = $_SESSION['username'] ")
............
}
does not work with $_SESSION['username']
That should work, asssuming $variable has a value. I use that syntax all the time.
thanks for the help.
but I think it is probably because it's in a preEdit function I defined.
You edited your post after I answered.
If you are passing $username in, then $_SESSION['username'] should be
$_SESSION[$username]. But that doesn't make much sense. Can you clarify what you are trying to do?
sorry that was confusing. so I tried two ways.
under preEdit
1) One way
function average_number_rating ($editor, $db, $id, $value) {
.............
db->sql("SELECT * FROM mytable
WHERE user = $_SESSION['username'] ")
............
}
2) second way
$username = $_SESSION['username']
function average_number_rating ($editor, $db, $id, $value, $username) {
.............
db->sql("SELECT * FROM mytable
WHERE user = $username ")
............
}
None of them works.
What is the message returned by the server?
Can you show your full and unabridged PHP please?
I'm also slightly unsure what you are actually trying to do. Do you just want to write the user name from the session to the database when the new row is created?
Allan
http://datatables.net/tn/1
I want to create a function under preEdit,
that uses $_SESSION['username'] to draw a query from database.
the query result should be corresponding to that user.
hi tangerine and Allan:
I have solved it. it was syntax issue.
thanks.
for anyone who has the same problem
use
sql("SELECT ....................
//your query
WHERE user = ' ".$_SESSION['username']." '
//
");
Thanks for posting back. Good to hear you've got it working.
Allan