1
I have the following query that picks up a date, was tested on phpmyadmin and 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 returns the error:
Fatal error: Call to a Member Function fetch_assoc() on Boolean in xxx
The error returned by the database via php is:
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
Code in 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'];
Try this and see if there are any mistakes,
$mysqli->query($PenultimaData)or die($mysqli->error);
– rray
The error that gives is: 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
– Paulo Rosa