$_SESSION['username'] in Server-side

$_SESSION['username'] in Server-side

celiahuangceliahuang Posts: 17Questions: 5Answers: 0

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

  • celiahuangceliahuang Posts: 17Questions: 5Answers: 0
    edited May 2017

    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']

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    "SELECT * FROM mytable WHERE user = '$variable' "
    

    That should work, asssuming $variable has a value. I use that syntax all the time.

  • celiahuangceliahuang Posts: 17Questions: 5Answers: 0

    thanks for the help.
    but I think it is probably because it's in a preEdit function I defined.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited May 2017

    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?

  • celiahuangceliahuang Posts: 17Questions: 5Answers: 0
    edited May 2017

    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.

  • allanallan Posts: 64,032Questions: 1Answers: 10,555 Site admin

    Json error occurs.

    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

  • celiahuangceliahuang Posts: 17Questions: 5Answers: 0

    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.

  • celiahuangceliahuang Posts: 17Questions: 5Answers: 0
    edited May 2017

    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']." '
    //
    ");

  • allanallan Posts: 64,032Questions: 1Answers: 10,555 Site admin
    Answer ✓

    Thanks for posting back. Good to hear you've got it working.

    Allan

This discussion has been closed.