I want to be able to return the 3 variables I have listed below, but when I try, I came into a dead end because I had no idea on how to return multiple entries from one function. A function can only return once, and when it does, it terminates, so what's the work around?
function M($username){
$query = "SELECT * FROM msg WHERE `to` = '".$this->userid."' && `to_viewed` = '0' && `to_deleted` = '0' ORDER BY `created` DESC";
$stmt = $this->connection->prepare($query);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$num_rows = $stmt->fetchColumn();
if($num_rows) {
$i=0;
$this->messages = array();
while($row = $data) {
// want to return these 3 for each result
$this->messages[$i]['id'] = $row['id'];
$this->messages[$i]['title'] = $row['title'];
$this->messages[$i]['message'] = $row['message'];
$i++;
}
} else {
return 1;
}
}
If I want to return the 3 variables, then the iteration would stop, and wouldn't continue until the loop is done.. can anyone help me out? I have been bashing my head over this for a bit.