Mysql variables do not work with php mysqli_fetch_array()

Asked

Viewed 108 times

-2

I set two Mysql widables to simplify one more Query that I want to deploy on my system. Using this command directly in SGBD, it works very well, however, when you want to use as a query string in my php script, it doesn’t work. Gives error in function mysqli_fetch_array(connected, queryString). What could be?

<?php 

$str = " /*Defino as variáveis*/    
SET @totVenda := (SELECT SUM(total) FROM tb_produto_item_estoque_saida); 
SET @totPorcentagem := (SELECT SUM((ROUND((total/@totVenda*100),2))) FROM tb_produto_item_estoque_saida); 

/*Query principal onde faço uso das variáveis*/
SELECT i.id, i.nome, SUM(ies.quantidade) AS quantidade, ies.valor, SUM(ies.total) AS total, 

@totVenda AS TotalGeral, 

SUM((ROUND((ies.total/@totVenda*100),2))) AS porcentagem

FROM tb_produto_item AS i 
LEFT JOIN tb_produto_item_estoque_entrada AS iee ON i.id = iee.idItem
LEFT JOIN tb_produto_item_estoque_saida AS ies ON i.id = ies.idItem 

GROUP BY i.id
ORDER BY porcentagem DESC";

$query = mysqli_query($conexao->conecta(),$str);

while($retorno = mysqli_fetch_array($query))
{
  //Listaria o resultado aqui! 
}

?>

Thank you from now on! Good week! :)

  • $query = mysqli_query($connected,$str); removes function connect()

  • Fabio, why don’t you create a trial in your bank? This will improve the running time and make the application faster, with the process created, in PHP you will just need to call it and ready.

  • Call each line separately, variables persist in the session. By default you cannot multiquery with the mysql_query

1 answer

4


The problem is that they are darlings separated:

  1. SET @totVenda := (SELECT SUM(total) FROM tb_produto_item_estoque_saida);
  2. SET @totPorcentagem := (SELECT SUM((ROUND((total/@totVenda*100),2))...
  3. SELECT i.id, i.nome, SUM(ies.quantidade) AS quantidade, ies.valor, ...

And the function mysqli_query, even for the sake of security, does not execute a string in this format.

If you want more than one query should use this function:

mysqli_multi_query($link, $query)

https://secure.php.net/manual/en/mysqli.multi-query.php

Or run separately:

mysql_query($link, "SET @totVenda := (SELECT SUM(total) FROM  ... );"); 
mysql_query($link, "SET @totPorcentagem := (SELECT ... uma por linha ... );")
mysql_query($link, "SELECT i.id, i.nome, SUM(ies.quantidade) AS ... ");

As long as you wear the same $link in all works, because the variables are retained by connection.

  • I was running some tests to see what would be best... the multi_query or several querys... in terms of performance doesn’t change much.. is almost equal to both cases... I can’t understand the advantage of using multi_query besides being able to make several queries at the same time! =/

  • 2

    @Andreicoelho is not a matter of advantage, but of what is best suited to each situation. It doesn’t change much if you test locally. If you take any kind of network latency, for example on separate servers, the "loose" queries will multiply the delay with each request, multi_query will send at once. Out of the security issue you make sure will only allow multiple queries only where really defined, minimizing the area for injection of certain types of command.

  • Thank you very much Bacco for that comment! I will give you more in depth

Browser other questions tagged

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