-1
I have some functions(down below) of a system of service evaluations. But when I run them in my index.php
the server takes too long to run them, the page takes almost 30s to return the data. Something is wrong:
index php.
<!-- avaliações -->
<?php
echo "Atendimento <br />";
echo $winfood->getPorcentNotas("atendimento")."<br /><br />";
echo "Comercial <br />";
echo $winfood->getPorcentNotas("comercial")."<br /><br />";
echo "Suporte: <br />";
echo $winfood->getPorcentNotas("suporte")."<br /><br />";
echo "Instalacao: <br />";
echo $winfood->getPorcentNotas("instalacao")."<br /><br />";
?>
<!--// avaliações -->
php class.
<?php
// função
public function getPorcentNotas($setor)
{
// conexão com banco de dados
$conexao = mysqli_connect($this->dbservidor,$this->dbusuario,$this->dbsenha) or die(mysqli_connect_error($conexao));
$select = mysqli_select_db($conexao,$this->dbnome) or die (mysqli_connect_error($select));
// for
$count = 5;
$i = 0;
do
{
// busca os dados
$query = mysqli_query($conexao, "SELECT COUNT(nota) AS total FROM avaliacoes WHERE setor_area = '$setor' AND nota = '$i'");
$retorno = mysqli_fetch_assoc($query);
$total = $retorno['total'];
$i++; // incrementa
}while($i < $count);
// contar as notas
$_1 = $this->getCount_Notas_ByNota($setor,"1");
$_2 = $this->getCount_Notas_ByNota($setor,"2");
$_3 = $this->getCount_Notas_ByNota($setor,"3");
$_4 = $this->getCount_Notas_ByNota($setor,"4");
$_5 = $this->getCount_Notas_ByNota($setor,"5");
//
echo "Horrivel: $_1 pessoa(s)<br />";
echo "Ruim: $_2 pessoa(s)<br />";
echo "Razoavel $_3 pessoa(s)<br />";
echo "Muito bom $_4 pessoa(s)<br />";
echo "Excelente: $_5 pessoa(s)<br />";
}
//
public function getCount_Notas_ByNota($setor,$nota)
{
// conexão com banco de dados
$conexao = mysqli_connect($this->dbservidor,$this->dbusuario,$this->dbsenha) or die(mysqli_connect_error($conexao));
$select = mysqli_select_db($conexao,$this->dbnome) or die (mysqli_connect_error($select));
$query = mysqli_query($conexao, "SELECT COUNT(nota) AS total FROM avaliacoes WHERE setor_area = '$setor' AND nota = '$nota'");
$retorno = mysqli_fetch_assoc($query);
$total = $retorno['total'];
return $total;
}
?>
Server
local
orremoto
?– NoobSaibot
the server is
local.
Easyphp– Emily Silva
This can have several factors, overloaded operating system, etc., etc. Take a test
remoto
to know if it really is the code.– NoobSaibot
Try to run SELECT in the database only once and optimize your loop. It is possible that you can get the same results with the following SQL
SELECT nota, COUNT(nota) AS total FROM avaliacoes WHERE setor_area = '$setor' GROUP BY nota
– William Urbano