So I have this function that takes all the users from an MySql table and stores them in an array:
public function graphVals() {
$sql = "SELECT user_username FROM users WHERE user_register_date > CURDATE() - 2592000";
if ($stmt = $this->connect->prepare($sql)) {
$stmt->execute();
$stmt->bind_result($username);
while ($row = $stmt->fetch()) {
$stmt->bind_result($username);
$data[] = $username;
}
$stmt->close();
foreach($data as $key) {
$length[] = sizeof($key);
}
print_r($data);
} else {
$error = true;
$message['error'] = true;
$message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
return json_encode($message);
}
}
After that, as you can see I want to return an array which contains the length of the usernames array.
But my question is, as you can see I’m fetching an array with all usernames, each one at one index, what I actually want to do is take all users registered on one date and store them in a single index, all users registered on the date after the previous one and store them in the next index, etc.
How would I do that ? Can I adjust this function to do that ?
This is easy to do :
Hope this helps 🙂