Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. $sql = 'SELECT COUNT(sexo) as total_sex, COUNT(id) as total FROM usuarios GROUP BY sexo';
  2.  
  3. <?php
  4. $driver = 'mysql';
  5. $database = "dbname=CODINGGROUND";
  6. $dsn = "$driver:host=localhost;unix_socket=/home/cg/mysql/mysql.sock;$database";
  7.  
  8. $username = 'root';
  9. $password = 'root';
  10.  
  11. try {
  12. $conn = new PDO($dsn, $username, $password);
  13. echo "<h2>Database CODINGGROUND Connected<h2>";
  14. }catch(PDOException $e){
  15. echo "<h1>" . $e->getMessage() . "</h1>";
  16. }
  17. $sql = 'SELECT COUNT(sex) as total_sex,COUNT(id) as total, sex FROM users GROUP BY sex';
  18. $stmt = $conn->prepare($sql);
  19. $stmt->execute();
  20.  
  21. $TotalRegistros = 0;
  22. $Total['F'] = 0;
  23. $Total['M'] = 0;
  24.  
  25. echo "<pre>";
  26. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  27. while($row = array_shift($rows)){
  28. $TotalRegistros = ($TotalRegistros + $row['total']);
  29. $Total[$row['sex']] = $row['total_sex'];
  30. echo "Total de " . $row['sex'] . ": " .$row['total_sex'];
  31. echo "<br>";
  32. }
  33.  
  34. echo "Registros: " . $TotalRegistros . "<br>";
  35. $PorcentagemMulheres = ( $Total['F'] / $TotalRegistros ) * 100;
  36. $PorcentagemHomens = ( $Total['M'] / $TotalRegistros ) * 100;
  37. echo "Porcentagem de Mulheres: " . number_format( $PorcentagemMulheres, 2 ) . '%' . "<br>";
  38. echo "Porcentagem de Homens: " . number_format( $PorcentagemHomens, 2 ) . '%' . "<br>";
  39. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement