统计php查询数据库[关闭]

问题描述:

I have to make a list of games based on the most votes. I must then use the count by the query to my db.Ho tried the code below but nothing happens. Help me

$con=mysqli_connect("localhost","root","","blog");
            // Check connection
            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            $result = mysql_query($con,"SELECT COUNT(nomeGioco) FROM voto");


            mysqli_close($con);

我必须根据最多的选票制作游戏列表。 我必须使用查询计数到我的db.Ho尝试下面的代码但没有任何反应。 帮帮我 p>

  $ con = mysqli_connect(“localhost”,“root”,“”,“blog”); 
 //检查连接
 if(mysqli_connect_errno()  ){
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
 
 $ result = mysql_query($ con,“SELECT COUNT(nomeGioco)FROM voto”); 
 
 
 mysqli_close($ con); 
  code>   pre> 
  div>

If you'd like to get a list of game names, along with the number of times that that name appears in the table, you'll want to start with the following query:

SELECT nomeGioco, COUNT(nomeGioco) num_count 
    FROM voto 
    GROUP BY nomeGioco 
    ORDER BY num_count DESC

To break it down a little, GROUP BY will aggregate the rows based on the 'nomeGioco' column, and "ORDER BY column DESC" will sort your rows by num_count in descending order (leaving the games occurring the most in the table at the top).

You can then use mysqli_fetch_all to get all of the results from that results object, like so:

$query = 'SELECT nomeGioco, COUNT(nomeGioco) num_count '
        . 'FROM voto '
        . 'GROUP BY nomeGioco '
        . 'ORDER BY num_count DESC';

$con = mysqli_connect("localhost","root","","blog");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con, $query);

$contents = mysqli_fetch_all($result, MYSQLI_ASSOC);

mysqli_free_result($result);
mysqli_close($con);

// The contents of the query
var_dump($contents);

// Go through each row and append them to a string to output in HTML
$toHTML = '';
if(is_array($contents)) {
    foreach($contents as $row) {
        $toHTML .= $row['nomeGioco'] . ': ' . $row['num_count'] . ' votes<br />';
    }
}

echo $toHTML;

make query

$con = New mysqli("localhost","root","","blog");
if (!$conn)
{
die("Database connection failed: " . mysqli_error());
}
$query = $con->query("SELECT COUNT(`nomeGioco`) FROM `voto`");
$result = $query->fetch_assoc();
echo $result['COUNT(nomeGioco)'];
$con->close;