Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $sql = 'SELECT COUNT(sexo) as total_sex, COUNT(id) as total FROM usuarios GROUP BY sexo';
- <?php
- $driver = 'mysql';
- $database = "dbname=CODINGGROUND";
- $dsn = "$driver:host=localhost;unix_socket=/home/cg/mysql/mysql.sock;$database";
- $username = 'root';
- $password = 'root';
- try {
- $conn = new PDO($dsn, $username, $password);
- echo "<h2>Database CODINGGROUND Connected<h2>";
- }catch(PDOException $e){
- echo "<h1>" . $e->getMessage() . "</h1>";
- }
- $sql = 'SELECT COUNT(sex) as total_sex,COUNT(id) as total, sex FROM users GROUP BY sex';
- $stmt = $conn->prepare($sql);
- $stmt->execute();
- $TotalRegistros = 0;
- $Total['F'] = 0;
- $Total['M'] = 0;
- echo "<pre>";
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- while($row = array_shift($rows)){
- $TotalRegistros = ($TotalRegistros + $row['total']);
- $Total[$row['sex']] = $row['total_sex'];
- echo "Total de " . $row['sex'] . ": " .$row['total_sex'];
- echo "<br>";
- }
- echo "Registros: " . $TotalRegistros . "<br>";
- $PorcentagemMulheres = ( $Total['F'] / $TotalRegistros ) * 100;
- $PorcentagemHomens = ( $Total['M'] / $TotalRegistros ) * 100;
- echo "Porcentagem de Mulheres: " . number_format( $PorcentagemMulheres, 2 ) . '%' . "<br>";
- echo "Porcentagem de Homens: " . number_format( $PorcentagemHomens, 2 ) . '%' . "<br>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement