devwalt Posted April 28, 2008 Share Posted April 28, 2008 This may be a vary dumb question with a very simple answer. I hope... This is a class project, yes I am new to php, however I am having fun. Anyway, my question is this: Can I use a SESSION variable in a PHP/MYSQLI statement to run a query on a database? I use the code below in other areas using $_POST, however, it does not seem work using SESSION. I take the where clause out and the table data prints fine. My goal is to get specific user information, from a user that is already logged in, not the whole table. The echo statement below prints fine, I put that there to verify the session passed the variable. <?php removed DB connection info if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } if($result=$DBConnect->query("SELECT User_ID, Username, Password FROM tbl_users WHERE Username = {$_SESSION['Username']}")) { while($fieldData=$result->fetch_object()) { echo 'Your ID:'.$fieldData->User_ID.'<br />'; echo 'Your Username:'.$fieldData->Username.'<br />'; echo 'Your Password:'.$fieldData->Password.'<br />'; } } echo $_SESSION['Username']; $DBConnect->close(); ?> Quote Link to comment Share on other sites More sharing options...
Cep Posted April 28, 2008 Share Posted April 28, 2008 Is the username a string? You may just need to add single quotes around your {$_SESSION['Username']} if that is the case. Quote Link to comment Share on other sites More sharing options...
devwalt Posted April 28, 2008 Author Share Posted April 28, 2008 That did it Cep. Thanks 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.