0

Please help me with how can I retrieve data from mysql having a variable in my sql query.. my php code is

$status=$_GET['status'];
$query="SELECT * FROM students WHERE status='$status' ";

When I run the query mysql generates

 unknown column 'Lead'

where Lead is the value of $status. I tried it with \'$status\' , '{$status}' but same error.. Please help me I'm working on it since yesterday but found no solution. I tried mysql_real_escape_string() but same error.. may be I was using it with wrong syntax.

  $query="SELECT * FROM students WHERE status=".mysql_real_escape_string($status);

Thanks in advance....

11
  • 1
    If you echo($query) what do you see? Commented Oct 8, 2012 at 21:03
  • 3
    $query="SELECT * FROM students WHERE `status`='" . mysql_real_escape_string($status) . "'"; Commented Oct 8, 2012 at 21:04
  • Keep in mind: stackoverflow.com/questions/60174/… (I recommend placeholders in general to avoid having to worry about quotes - I'm lazy.) Commented Oct 8, 2012 at 21:05
  • Are you sure you don't have "where $status='$status'"? Those pesky symbols like to jump around sometimes. Commented Oct 8, 2012 at 21:07
  • @NullUserException that was an answer, you should've typed it as such! Commented Oct 8, 2012 at 21:08

2 Answers 2

1
$status=$_GET['status'];
$query="SELECT * FROM students WHERE status='".$status."'";

and escaping is a good idea, but I think mysql_escape_string is deprecated and its usage is discouraged, you should always use mysql_real_escape_string.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm afraid 'status' is a reserved word for sql: see http://drupal.org/node/141051

Try:

$status = mysql_real_escape_string($status)

$query = "SELECT * FROM `students` WHERE `status` = '$status'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.