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

Sikander
  • 447
  • 4
  • 26

2 Answers2

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
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'";

Aubin
  • 14,617
  • 9
  • 61
  • 84