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?
use mysqli_, mysql_ is being discontinued.
– Ivan Ferrer
Good afternoon @Ivanferrer , I know hehe , I’m still studying php , so I haven’t ventured with mysql_i and PDO.
– Henrique Felix
mysqli is almost the same, only improved and safer.
– Ivan Ferrer
Yes, I’m seeing the PDO more, I liked it more. As for this situation , what can I do to work ? Since grateful ja
– Henrique Felix
You can create an alias: sum(entries) as total, and retrieve it
$row['total']
.– Ivan Ferrer
Moreover, the word
data
is reserved in Mysql, I suggest you replace with "data_reg".– Ivan Ferrer
Good afternoon , actually this as 'data_cadastro' hehe . I will try the suggestion of @rray , and put here as it was .
– Henrique Felix