PHP查询仅返回四分之一

时间:2019-01-11 13:00:47

标签: php sql heidisql

该查询应该返回四行:这是四个状态为50的人(在应用程序中为“产假”)。但是它只返回一个。

在HeidiSQL上,查询甚至没有运行,因为它显示

  

第13行的语法错误:

     

(...)

     

与您的MariaDB服务器版本对应,以在'a.id_regiao ='$ id_regiao'附近使用正确的语法               AND a.cod_status = 50               AND a.status'在第13行* /“

这是查询。我慢慢地熟悉sql语句,并且在询问之前确实在SO上进行了大量搜索:

//SELECTING PROJECT DATA
$query = "SELECT b.id_clt,b.nome AS nome_clt,
        a.id_evento AS a_id_evento,a.data AS a_data,a.data_retorno AS a_data_retorno,
        c.id_evento AS c_id_evento,c.data AS c_data,c.data_retorno AS c_data_retorno,
        (SELECT nome FROM projeto WHERE id_projeto = a.id_projeto) AS nome_projeto,
        (SELECT nome FROM curso WHERE id_curso = b.id_curso) AS nome_curso,
        DATE_FORMAT(a.data,'%d/%m/%Y') AS a_data_br,
        DATE_FORMAT(a.data_retorno,'%d/%m/%Y') AS a_data_retorno_br,
        DATE_FORMAT(c.data,'%d/%m/%Y') AS c_data_br,
        DATE_FORMAT(c.data_retorno,'%d/%m/%Y') AS c_data_retorno_br
        FROM rh_eventos AS a
        INNER JOIN rh_clt AS b ON (a.id_clt = b.id_clt AND a.cod_status = 50)
        LEFT JOIN rh_eventos AS c ON (b.id_clt = c.id_clt AND c.cod_status = 54)
        WHERE $cond_projeto a.id_regiao = '$id_regiao' 
        AND a.cod_status = 50
        AND a.status = 1 
        AND NOW() BETWEEN a.data AND a.data_retorno 
        ORDER BY nome_projeto,b.nome;";

2 个答案:

答案 0 :(得分:3)

问题在查询中在这里

    WHERE $cond_projeto a.id_regiao = '$id_regiao' 

这将插入没有正确语法的变量(或可能是完整测试?)。如果它是变量,则在条件中包括表的列名。如果是完整测试,请像这样包含AND:

    WHERE $cond_projeto AND a.id_regiao = '$id_regiao' 

但是要当心!使用准备好的语句,您的代码现在似乎容易受到SQL注入攻击的影响(并且不要被那些琐碎的事情所困扰)。

答案 1 :(得分:0)

这里是查询,(如在其前面使用回显所示)。我现在可以在heidsql上看到输出。现在最好检查一下:

SELECT b.id_clt,b.nome AS nome_clt, 
a.id_evento AS a_id_evento,a.data AS a_data,a.data_retorno AS a_data_retorno,
 c.id_evento AS c_id_evento,c.data AS c_data,c.data_retorno AS c_data_retorno,
  (SELECT nome FROM projeto WHERE id_projeto = a.id_projeto) AS nome_projeto, 
  (SELECT nome FROM curso WHERE id_curso = b.id_curso) AS nome_curso, 
  DATE_FORMAT(a.data,'%d/%m/%Y') AS a_data_br, 
  DATE_FORMAT(a.data_retorno,'%d/%m/%Y') AS a_data_retorno_br,
   DATE_FORMAT(c.data,'%d/%m/%Y') AS c_data_br, 
    DATE_FORMAT(c.data_retorno,'%d/%m/%Y') AS c_data_retorno_br 
    FROM rh_eventos AS a 
    INNER JOIN rh_clt AS b ON (a.id_clt = b.id_clt AND a.cod_status = 50) 
    LEFT JOIN rh_eventos AS c ON (b.id_clt = c.id_clt AND c.cod_status = 54) 
    WHERE a.id_regiao = '1' AND a.cod_status = 50
    AND a.status = 1 
    AND NOW() BETWEEN a.data AND a.data_retorno ORDER BY nome_projeto,b.nome;

我现在可以在heidsql上看到输出,尽管我仍然无法弄清为什么它确实带来了其他三行。