How to View SQL Query in an Array

0

I have a question. When I run a Select Count in the database it displays everything right. But how to display the same way using PHP?

select country, count(*) as quantidade from customer group by country

$sql=mysql_query("select country, count(*) as quantidade from customer group by country");

And then I do not know.

    
asked by anonymous 30.04.2015 / 01:33

3 answers

3

You can do this as follows:

$arr = array();
$sql=mysql_query("select country, count(*) as quantidade from customer group by country");
while ($row = mysql_fetch_array($sql)) {
    $arr [$row ["country"]] = $row["quantidade"];
}

In this way, an array will be created where the country names are the keys and quantities are the values.

On this link there is more information on the command mysql_fetch_array .

    
30.04.2015 / 03:40
1

try using the mysql_fetch_array command:

$sql=mysql_query("select country, count(*) as quantidade from customer group by country");
$exibe = mysql_fetch_assoc($sql);
            echo $exibe["country"];

Then just put it inside a loop and do some testing.

    
30.04.2015 / 03:15
0

Use print_r with bank return within HTML tags <pre> as follows:

$query=mysql_query("select country, count(*) as quantidade from customer group by country");
$valores = mysql_fetch_assoc($query);
echo '<pre>';
print_r($valores);
echo '</pre>';
    
02.05.2015 / 15:45