Mysql语法错误无效

时间:2014-12-11 22:33:15

标签: php mysql

我想从mysql中获取一些价值

          $email = $_SESSION['email'];
          $email = mysql_real_escape_string($email); 
          $depst = "SELECT dept FROM  stud_reg WHERE email='$email'";
          $query="SELECT * FROM  events WHERE `group`='$depst'";

但我得到了一个未知的错误。

      You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '**@**.com''' at line 1

虽然我没有发现任何语法错误

2 个答案:

答案 0 :(得分:2)

由于您不执行第一个查询,只需在最后一个语句中将其用作子查询:

更改

$query="SELECT * FROM  events WHERE `group`='$depst'";

$query="SELECT * FROM  events WHERE `group`=($depst)";

答案 1 :(得分:1)

您的代码似乎容易受到SQL注入攻击(请read this获取更多信息)。你应该使用预备语句来做这种事情:

$mysqli->prepare("SELECT dept FROM  stud_reg WHERE email=?");
$stmt->bind_param("s", $email);
$stmt->execute();

参考文献: