jim.davidson Posted July 19, 2010 Share Posted July 19, 2010 I'm trying to make a query from user input and I'm not sure how to put the variable in the query. Here's an example $_SESSION['email_query'] contains community = 'on' mysql_select_db($database_mine, $mine); $query_listEmail = "SELECT needs_id, email_id, status, name, email FROM needs ORDER BY email_id DESC WHERE $_SESSION['email_query']"; $ListEmail = mysql_query($query_listEmail, $mine) or die(mysql_error()); I'm pretty sure I need something like single or double quotes around $_SESSION['email_query'] in the where claus I will be greatfull for any help Link to comment https://forums.phpfreaks.com/topic/208194-need-some-help-with-writing-a-query/ Share on other sites More sharing options...
Stalingrad Posted July 19, 2010 Share Posted July 19, 2010 Try setting the session as a variable first... that's the first thing I can think of. Link to comment https://forums.phpfreaks.com/topic/208194-need-some-help-with-writing-a-query/#findComment-1088196 Share on other sites More sharing options...
Kevin.Arvixe Posted July 19, 2010 Share Posted July 19, 2010 $email_query = mysql_real_escape_string($_SESSION['email_query']; SELECT needs_id, email_id, status, name, email FROM needs WHERE email_query = " . $email_query . " ORDER BY email_id DESC" 1. Whenever you use a WHERE clause, it has to be comparitive. So the variable has to be compared to a row in the database. 2. mysql_real_escape_string(); -- Please be nice to your server admins... Link to comment https://forums.phpfreaks.com/topic/208194-need-some-help-with-writing-a-query/#findComment-1088200 Share on other sites More sharing options...
jim.davidson Posted July 19, 2010 Author Share Posted July 19, 2010 Ok, When I put the session variable to a variable and change the order of my query to put the where clause before the order by clause it works. Thanks for the suggestions Link to comment https://forums.phpfreaks.com/topic/208194-need-some-help-with-writing-a-query/#findComment-1088219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.