xxreenaxx1 Posted February 15, 2011 Share Posted February 15, 2011 I want to use session to do a query and will I be able to do this? I have a session that was gathered from login and now i was to use this session to do a query If Yes, How? Quote Link to comment https://forums.phpfreaks.com/topic/227733-can-you-use-session-to-retrieve-mysql-data/ Share on other sites More sharing options...
trq Posted February 15, 2011 Share Posted February 15, 2011 The $_SESSION array shouldn't really be treated any differently to any other array. $sql = "SELECT foo FROM bar WHERE boo = '{$_SESSION['whatever']}'"; Of course, if it is user submitted data within the $_SESSION array you will want to make sure to sanitize it. Quote Link to comment https://forums.phpfreaks.com/topic/227733-can-you-use-session-to-retrieve-mysql-data/#findComment-1174477 Share on other sites More sharing options...
xxreenaxx1 Posted February 15, 2011 Author Share Posted February 15, 2011 my query is sort of messed up. But when I try it on mysql it works. Not sure how to implement that on to PHP. so far I have <Html> <?php MYSQL_CONNECT(localhost,'root','') OR DIE("Unable to connect to database"); @mysql_select_db(Examination) or die( "Unable to select database"); ?> <form action="try1.php" method="post"> <? //$query=("SELECT * FROM subject"); $query=("SELECT DISTINCT * FROM User u, User_X_Subject us, Subject s WHERE u.Use_Name = '{$_SESSION['username']}' AND u.Use_ID= us.Use_ID AND s.Sub_ID=us.Sub_ID"); $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); echo "<select name=myselect>"; while($row=mysql_fetch_array($result)){ echo "<OPTION VALUE=".$row['s.Sub_ID'].">".$row['s.Sub_Name']."</OPTION>"; } echo "</select>"; ?> <input type="submit" value="submit"/> </form> </html> but ofcourse its NOT going to work and its NOT working. Help me out Quote Link to comment https://forums.phpfreaks.com/topic/227733-can-you-use-session-to-retrieve-mysql-data/#findComment-1174498 Share on other sites More sharing options...
trq Posted February 15, 2011 Share Posted February 15, 2011 Firstly, don't use <? tags, ever. Always use <?php Secondly, you have no call to session_start. Thirdly, values being assigned to variables do not need () braces. $foo=('blah'); should simply be $foo = 'blah'; die is a poor method of error trapping. Check for errors within an if() statement and handle gracefully. Lastly, what errors are you getting and have you tried echoing your query to see what is actually looks like? Quote Link to comment https://forums.phpfreaks.com/topic/227733-can-you-use-session-to-retrieve-mysql-data/#findComment-1174515 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.