I have the following function :
//Counts The Users In The Database Per Level And Stores The Results In An Array
public function countUsers() {
$users = array();
$length = array();
for( $i = 0; $i >= 2; $i++){
$sql = "SELECT user_id FROM users WHERE user_level = '$i'";
if ($stmt = $this->connect->prepare($sql)) {
$stmt->bind_result($id);
$stmt->execute();
while ($row = $stmt->fetch()) {
$stmt->bind_result($id);
$users[] = $id;
}
$stmt->close();
$length[] = sizeof($users);
} else {
$error = true;
$message['error'] = true;
$message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
return json_encode($message);
}
}
return $length;
}
This function is suppose to loop through the table and store the id’s in an array for each level and then get the sizeof each array per level. I mean, I have 10 user for level 0, 23 for level 1 and so on, and I need this info stored in an array.
What is wrong with my function ?
Can you have the SQL do more of the work for you?
That will give you all the counts for each user level.