计算表列中的唯一值 - 结果始终为1

时间:2015-08-18 20:21:12

标签: php mysql

我想知道是否有人能够发现我在哪里出错?我想返回列的唯一值的数量。当我在Phpmyadmin上运行此查询时,结果是正确的,但是当我尝试通过PHP执行此操作时,我会继续获得结果1.

//Find out number of unique slotids and assign to variable
    $q2= "SELECT COUNT( DISTINCT(`slotid`) ) FROM `individualavailability`";
    $result2 = mysqli_query ($dbcon, $q2);
    $count = mysqli_num_rows ($result2);
    echo $count. " slot ids";

2 个答案:

答案 0 :(得分:2)

mysqli_num_rows函数返回从服务器读取的行数,而不是查询计算的slotid数。这样的事情会更好:

import pickle

with open("data.dat", "w") as f:
    p = pickle.Pickler(f, 1)
    p.dump([1, 2, 3, 4, 5])
    p.dump("abcdefg")

答案 1 :(得分:0)

$query=("SELECT count(DISTINCT (`slotid`)) as total from `individualavailability`");
$result=mysqli_query ($dbcon, $query);
$row=mysqli_fetch_assoc($result);
echo $row['total'];