Jump to content

Need some help with writing a query


jim.davidson

Recommended Posts

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

$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...

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.