如何通过PHP使用group来计算mysql?

时间:2016-07-20 06:09:09

标签: php mysql

如何使用PHP使用group来计算mysql?

当我测试此代码时,它是echo 1

如何处理echo 3(按计数国家/地区)?

这是我的table_test

_____________________
|__id___|__country__|
|__1____|____usa____|
|__2____|____usa____|
|__3____|___china___|
|__4____|____uk_____|

这是我的代码。

<?PHP
$result = mysql_query("SELECT COUNT(*) FROM table_test GROUP BY country");
$row = mysql_fetch_row($result);
mysql_free_result($result);
$all_country = $row[0];
echo $all_country;
?>

3 个答案:

答案 0 :(得分:3)

尝试:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="14dp" />
<gradient
    android:angle="45"
    android:centerColor="#1FA8D1"
    android:centerX="35%"
    android:endColor="#060d96"
    android:startColor="#0e7e1d"
    android:type="linear" />
<padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />
<size
    android:width="270dp"
    android:height="60dp" />
<stroke
    android:width="3dp"
    android:color="#000000" />

答案 1 :(得分:0)

1)使用mysqli_* API代替mysql_* API

2)使用mysqli_num_rows获取总行数

$sql="SELECT COUNT(*) FROM table_test GROUP BY country";

 if ($result=mysqli_query($con,$sql))
 {
    $rowcount=mysqli_num_rows($result);
 }

echo $rowcount; // print 3

答案 2 :(得分:0)

使用此代码计算所选查询的行数

 <?php
$result = mysql_query("SELECT COUNT(*) FROM table_test GROUP BY country");
$row = mysql_fetch_row($result);
$all_country = mysql_num_rows($result);
echo $all_country;
?>