PHP中的SQL查询

时间:2014-04-27 16:35:26

标签: php mysql xampp

我的数据库中有两个表,具有以下结构

1.ps_branch

COLUMNS:ps_code a1 a2 a3 a4 a5 a6 a7

2.students_info

COLUMNS:std_name std_cg st_code

我正在使用xampp服务器。

我在php中写了一个查询:

$qs1="select * from station_details where st_code IN 
(
( select DISTINCT st_code FROM students_info , ps_branch WHERE ps_code=st_code AND    std_cg >".$_POST['Cgpa'].
      "AND".$_POST['branch']."=".'1'."))";
$query1=mysqli_query($con1,$qs1);

但查询显示错误。 当我直接在phpmyadmin mysql中查询以下内容时,我得到了我需要的答案

select * from station_details where st_code IN 
(
( select DISTINCT st_code FROM students_info , ps_branch WHERE ps_code=st_code AND  std_cg>6
      AND a7=1)
 )

$ _ POST ['branch']将给出一个值a1 a2 ...或a7 $ _POST ['Cgpa']是一个数值

1 个答案:

答案 0 :(得分:2)

' AND'

周围需要额外的空格
$qs1="select * from station_details where st_code IN 
(
( select DISTINCT st_code FROM students_info , ps_branch WHERE ps_code=st_code AND std_cg >".$_POST['Cgpa'].
      " AND ".$_POST['branch']."=".'1'."))";
$query1=mysqli_query($con1,$qs1);

调试方法是在设置$ qs1之后和执行查询之前使用die($qs1);显示$ qs1的值。然后你就可以看到问题了。