Query string works in Phpmyadmin and does not work in PHP

Asked

Viewed 131 times

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);

  • 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

1 answer

2


The method query() executes only one SQL statement at a time, the query sends two of them, the first is the variable definition the second select, the delimitation is done by semicolon.

The simplest way to solve this is to break the instructions (when possible) and send them individually.

$mysqli->query("set @rn:=0,@grp:=0,@prevdate:='';");

$sql = "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($sql);
$Resultado = $Rs->fetch_assoc();
print_r($Resultado);

Another way for more complex cases is to use multiple_query() which allows the sending of several queries.

//envia 3 consultas para o banco
$db->multi_query('set @teste = 2015; select @teste + 1 as ano; select @teste + 1 as ano;') or die($db->error);

do{
    //armazena o resultada consulta
    if($result = $db->store_result()){
        while($row = $result->fetch_assoc()){// extrai o resultado do resource
            echo $row['ano'] .'<br>';
        }
    }   
}while($db->more_results() && $db->next_result());
// primeiro verifica se existem mais resultados se sim, 'avança' para o próximo.
  • Using the, mysqli_multi_query statement?

  • @Paulorosa can use yes, have an additional work soon edit the answer with an example of her.

  • Excellent :) +1

  • Dear rray. Thank you very much. It worked. Thank you for sharing your knowledge. Provide.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.