Error registration report on method return

Asked

Viewed 51 times

0

I’m having a problem I haven’t been able to solve in over a week.

I wish to make a registration report for a particular website.

In the report, I want to list the cities and their total registrations.

Previously, I would do a query without using a method, only the function mysql_query() PHP where I would capture the data in the Mysql database, list the records with the type loop while onscreen. However, I want to improve this, as this system requires me to use a method over and over again, I want to adjust it using a method that serves as an instrument for reusing the code.

However, I am not managing to make the result that is within the while(...) appear correctly in my method.

The structure of the code I’m cracking my head on:

$uf = $_POST['uf'] //aqui, $uf recebe 1 ou mais estados vai post

$x = count($uf);

for($i = 0 ; $i < $x; $i++){

    $funcao = cad_por_uf($uf[$i],$dataInicial,$dataFinal); // aqui ,passo o valor do return da funcao para a variavel $funcao;

    echo $funcao[0].'-'.$funcao[1];
}


function cad_por_uf($uf,$dataInicial,$dataFinal){

    $sql = mysql_query("SELECT sum(cadastros),uf,cidade FROM tblCadastros where data >= '$dataInicial' and data <= '$dataFinal' and uf = '$uf' and deletada = 0 group by (cidade)");
    while($row = mysql_fetch_array($sql)){
        $cidade = $row['cidade'];
                $cadastros= $row['sum(cadastros)'];                

                return array($cidade,$cadastros);

    }

}

When displaying the variable $funcao[0], should list all the cities in the state. But, he’s just returning me 1 result. How could solve this problem?

  • 1

    use mysqli_, mysql_ is being discontinued.

  • Good afternoon @Ivanferrer , I know hehe , I’m still studying php , so I haven’t ventured with mysql_i and PDO.

  • mysqli is almost the same, only improved and safer.

  • Yes, I’m seeing the PDO more, I liked it more. As for this situation , what can I do to work ? Since grateful ja

  • You can create an alias: sum(entries) as total, and retrieve it $row['total'].

  • Moreover, the word data is reserved in Mysql, I suggest you replace with "data_reg".

  • Good afternoon , actually this as 'data_cadastro' hehe . I will try the suggestion of @rray , and put here as it was .

Show 2 more comments

2 answers

1

The correct in this case would be to do your query this way:

Obs: traded date for data_cadastre, because date is a reserved word of Mysql and will therefore return with error. Another detail is how to declare array. You declare in two ways: $array = []; (version 4.5 or higher) or $array = array();. Another thing is it makes no sense to pass the connection into the method of mysql_query(). I adjusted the method with the standard PSR-1 and PSR-2.

function cadPorUf($uf, $initialDate, $finalDate)
{

   $sql = "SELECT sum(cadastros) as total,
           uf,
           cidade FROM tblCadastros
           WHERE data_cadastro
           BETWEEN '$initialDate' and '$finalDate'
           AND uf = '$uf'
           AND deletada = 0
           GROUP BY cidade";
    //faça um upgrade para PDO (vamos colaborar para melhorar e não piorar os códigos)
    $query = mysql_query($sql);
    $list = array();
    while ($row = mysql_fetch_array($query)) {
        $list[] = $row;
    }
    return $list;
}
  • If it is a function it makes sense yes to pass the connection, all globlais variables are accessible within functions?

  • Then , I was not passing the $connection ,and it was still working. The correct thing would be to pass it anyway ?

  • @Henriquefelix is a class or just a function?

  • @rray , is only a function.I call the page that connects to my controller.php. And on the controller.php page , I include the other pages of the site .

  • Taking advantage ,the result of both worked , but I’m using _fetch_assoc , because the array returned more friendly. : Array ( [0] => Array ( [sum(entries)] => 1 [Uf] => SP [city] => Itapeva ) [1] => Array ( [sum(entries)] => 1 [Uf] => SP [city] => ALTAIR ) [2] => Array ( [sum(entries)] => 1 [Uf] => SP [city] => high glade ) etc etc

  • the difference between..._fetch_array and .. fetch_assoc, is that array, vc can use both numeric and associative key, whereas the ...fetch_assoc, vc is required to capture by the associative key.

  • Vish, I really didn’t know this, and in this case ,fetch_assoc goes down better , why so , when displaying the array , it only shows the database fields and not the Dice and then the field name .

  • if you do this, it already solves the problem: mysql_fetch_array($query, MYSQL_ASSOC)

  • the word connection there is not being used. Just to warn...

Show 4 more comments

0


Return all rows after while do not need to create a new array, use an alias for the calculation.

function cad_por_uf($uf,$dataInicial,$dataFinal, $conexao){
    $consulta = "SELECT sum(cadastros) as cadastros, uf, cidade
                 FROM tblCadastros
                 WHERE data >= '$dataInicial' and data <= '$dataFinal' and uf = '$uf' and deletada = 0 group by (cidade)";

    $sql = mysql_query($consulta, $conexao);
    $lista = array();
    while($row = mysql_fetch_assoc($sql)){
        $lista[] = $row;
    }
    return $lista;
}
  • good afternoon @rray , I will test here and already put if it worked .

  • What’s the problem with the answer?

  • Good afternoon, it worked out here ! The only problem he had was that when he print the variable $funcao[numero array] he returns , [0] => 0 , [entries] => 0 , [1] => SP , [Uf] => SP , [2] => sao paulo , [ city] => sao paulo ) etc. Not ?

  • @Henriquefelix, you want the return as?

  • If possible, return it to the array , the name of the database field and its value , type , ([entries] => 0 ; [Uf] => SP , etc )

Browser other questions tagged

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