grantp22 Posted October 28, 2009 Share Posted October 28, 2009 Hi Can somebody please give me the corect syntax for the following query, I am finding it difficult to pass the username of a session in a query. I have tried so many ways to implement this, but it just won't work... These are some of the methods I have tried! What am I doing wrong. It's in PHP by the way! $q = "SELECT user, message, senttime " ."FROM ".TBL_AUCTION_RESPONSES." WHERE user=".$session->username." ORDER BY senttime DESC"; $q = "SELECT user, message, senttime " ."FROM ".TBL_AUCTION_RESPONSES." WHERE user='.$session->username.' ORDER BY senttime DESC"; $q = "SELECT user, message, senttime " ."FROM ".TBL_AUCTION_RESPONSES." WHERE user='<?php echo $session->username;?>' ORDER BY senttime DESC"; If I use a string in the query as seen below in bolded letters, the query works just fine! $q = "SELECT user, message, senttime " ."FROM ".TBL_AUCTION_RESPONSES." WHERE user='grant' ORDER BY senttime DESC"; Can somebody tell me what the syntax should be for variables in a case like this Thanks Grant Link to comment https://forums.phpfreaks.com/topic/179279-passing-session-variables-in-a-query/ Share on other sites More sharing options...
DavidAM Posted October 28, 2009 Share Posted October 28, 2009 Session variables are stored as an array: $q = "SELECT user, message, senttime " . "FROM " . TBL_AUCTION_RESPONSES . " WHERE user=" . $_SESSION['username'] . " ORDER BY senttime DESC"; Link to comment https://forums.phpfreaks.com/topic/179279-passing-session-variables-in-a-query/#findComment-945920 Share on other sites More sharing options...
grantp22 Posted October 28, 2009 Author Share Posted October 28, 2009 davidAM I tried that out, it doesn't work either, it's something to do with the syntax. Should it be single quotes doubles, should it be structured differently, I tried so many different ways, it just won't work but should. Thanks for teaching me something, I never knew session variables were stored in an array,, i'm only using php and mysql for 3 weeks now! Link to comment https://forums.phpfreaks.com/topic/179279-passing-session-variables-in-a-query/#findComment-945942 Share on other sites More sharing options...
xtopolis Posted October 28, 2009 Share Posted October 28, 2009 Try $q = "SELECT user, message, senttime FROM " . TBL_AUCTION_RESPONSES . " WHERE user='" . $_SESSION['username'] . "' ORDER BY senttime DESC"; Link to comment https://forums.phpfreaks.com/topic/179279-passing-session-variables-in-a-query/#findComment-945951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.