Reputation: 51
I am wondering if someone might be able to spot where I'm going wrong here? I want to return the number of unique values for a column. When I run this query on Phpmyadmin the result is correct, but when I try to do it through PHP I keep getting the result 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";
Upvotes: 1
Views: 1300
Reputation: 73306
The mysqli_num_rows function returns the number of rows you read from the server, not the number of slotid counted by your query. Something like this should work better:
$q2= "SELECT COUNT( DISTINCT(`slotid`) ) as cnt FROM `individualavailability`";
$r2 = mysqli_query ($dbcon, $q2);
$row = mysqli_fetch_assoc($r2);
echo $row["cnt"];
Upvotes: 2
Reputation: 51
$query=("SELECT count(DISTINCT (`slotid`)) as total from `individualavailability`");
$result=mysqli_query ($dbcon, $query);
$row=mysqli_fetch_assoc($result);
echo $row['total'];
Upvotes: 0