I'm trying to use an object to count the number of records in my db that meet certain criteria but I can't work out how to make it work. The following code is my count method inside my db connection class:
//Counts the total number of records and returns integer
public function totalCount($fieldname, $tablename, $where = "")
{
$q = "SELECT count(".$fieldname.") FROM "
. $tablename . " " . $where;
$result = $this->mysqli->query($q);
$count = 0;
if ($result) {
while ($row = mysqli_fetch_array($result)) {
$count = $row[0];
}
}
return $count;
}
This code is outside the class:
//New instantiation
$mydb = new myDBC();
//Count the number of transactions for the user...
$count = $mydb->totalCount('amount','transactions','WHERE userid = 13');
//output the number of transactions for the user
print $count;
For some reason this always displays 0, even though my db has thousands of records inside it that should be counted. Grateful for any pointers as to where my syntax isn't right.
Thanks