sfia Posted April 25, 2020 Share Posted April 25, 2020 (edited) $sql = "SELECT id,username, date1, date2, total FROM datetable WHERE username = '".$_SESSION['uid']['username'] ."'"; Hello again, I ran into problem when trying to pull information for logged in user. I'm trying to display results for current user only who is signed in. Now I do have session_start(); and my query works if I replace '".$_SESSION['uid']['username'] ."'"; with 'specificusername' but when I run my current query displayed above it returns me all records with username = empty (I have some empty usernames for testing reasons). So that tells me that query cant find unique user name that is logged in. I don't get any errors and query still goes through. My datetable structure is My uid is defined in another document like so and uses another table called "users" session_start(); $_SESSION['id'] = $row['idUsers']; $_SESSION['uid'] = $row['uidUsers']; $_SESSION['email'] = $row['emailUsers']; header("Location: ../member.php?login=success"); exit(); This query works on another page where it displays logged in username in html document. <?php echo $_SESSION['uid']; ?> I have tried many different ways yesterday to modify my request ( WHERE username = $_SESSION['uid']; WHERE username = '".$_SESSION["uid"]) many other variations, but no matter what never returns logged in user information only empty username records. Can anyone see a problem with that query? or is it due to something else I'm missing? Sorry, maybe this goes in MySQL thread not here in PHP page. If someone can, please move it. Thanks. Edited April 25, 2020 by sfia comment on where to post this topic Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 25, 2020 Share Posted April 25, 2020 Did you echo $_SESSION['uid']['username']; to make sure it contains what you expect? Better yet: echo "<pre>"; var_dump($_SESSION); echo "</pre>"; Quote Link to comment Share on other sites More sharing options...
sfia Posted April 25, 2020 Author Share Posted April 25, 2020 I just used echo "<pre>"; var_dump($_SESSION); echo "</pre>"; and it returns NULL. So I'm missing my session value somehow on this page. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 25, 2020 Share Posted April 25, 2020 Do you have session_start on each page? Quote Link to comment Share on other sites More sharing options...
sfia Posted April 25, 2020 Author Share Posted April 25, 2020 <?php session_start(); $servername = ""; $username = ""; $password = ""; $dbname = ""; $connect = mysqli_connect($servername, $username, $password,$dbname); $sql = "SELECT id,username, date1 FROM datetable WHERE username = '".$_SESSION['uid']['username'] ."'"; $result = mysqli_query($connect, $sql); $json_array = array(); while($row = mysqli_fetch_assoc($result)) { $json_array[] = [ $row['id'], $row['username'], $row['date1'] $TotalTime ]; } echo json_encode($json_array); ?> Yes, session_start on each page. I played around a little and edit some things, I do get this now when run the var_dump($_SESSION); code. But my json format disappears. [] array(4) { ["id"]=> int(34) ["uid"]=> string(8) "bostin21" ["email"]=> string(18) "emaill123@gmail.com" ["user"]=> NULL } If i removed var_dump($_SESSION); than my query runs again. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 25, 2020 Share Posted April 25, 2020 Now uid exists but you have not made its value an array (it is just a string) so there is no element named 'username'. Quote Link to comment Share on other sites More sharing options...
sfia Posted April 25, 2020 Author Share Posted April 25, 2020 ok, I added $uid1 = $_SESSION['uid']; and than $sql = "SELECT id,username, date1 FROM datetable WHERE username = '$uid1'"; and that works!! Hopefully its the right way. Quote Link to comment Share on other sites More sharing options...
sfia Posted April 25, 2020 Author Share Posted April 25, 2020 Thank you for your guidance! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.