I have the following query that takes a date, has been tested in phpmyadmin and in the HeidiSQL program successfully:
Query Mysql
set @rn:=0,@grp:=0,@prevdate:='';
from
(select DataEncomenda,
@rn:=@rn+1 as rownum,
@grp:=if(@prevdate=DataEncomenda,@grp,@grp+1) as descGrp,
@prevdate:=DataEncomenda as unused
from encomendas
order by DataEncomenda DESC
) inR
where descGrp=2 group by descGrp
When I try to use it in php it returns the error:
Fatal error: Call to a member function fetch_assoc () on boolean in xxx
The error returned by the bank via php is:
You have an error in your SQL syntax; SELECT Data FROM (SELECT Data Order, @rn: = @ rn + 1 as rownum 'at line 2
PHP code:
$PenultimaData = "
set @rn:=0,@grp:=0,@prevdate:='';
SELECT DataEncomenda
FROM
( SELECT DataEncomenda,
@rn:=@rn+1 as rownum,
@grp:=if(@prevdate=DataEncomenda,@grp,@grp+1) as descGrp,
@prevdate:=DataEncomenda as unused
FROM encomendas
ORDER by DataEncomenda DESC
) inR
where descGrp=2 group by descGrp";
$Rs = $mysqli->query($PenultimaData);
$Resultado = $Rs->fetch_assoc();
$PenulData = $Resultado['DataEncomenda'];