Tenho a seguinte query que retoma uma data, foi testada no phpmyadmin e no programa HeidiSQL com sucesso:
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
quando tento utiliza-la no php devolve o erro:
Fatal error: Call to a member function fetch_assoc() on boolean in xxx
O erro retornado pelo banco via php é:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT DataEncomenda FROM ( SELECT DataEncomenda, @rn:=@rn+1 as rownum' at line 2
Código em PHP:
$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'];
$mysqli->query($PenultimaData)or die($mysqli->error);