I’m gettings started using mysqli prepared statements and am trying to write a class method that retrieves records matching certain date criteria, but does not display them — i want to be able to format the result outside of the class.
i have the class method working when it displays the results:
public function periodReceipts(){
global $db;
if($query = $db->prepare("SELECT * FROM receipts WHERE date BETWEEN ? AND ? ORDER BY date ASC")){
$query->bind_param("ss", $this->date1, $this->date2);
$query->execute();
$query->bind_result($id, $user_id, $vendor, $amount, $cat, $date);
$query->fetch();
while($query->fetch()){
echo "$id, $user_id, $vendor, $amount, $cat, $date <br/>";
}
$query->close();
}
}
and i have a similar method working that performs a mysqli query, retrieves records, calculates a total, and returns that result, unformatted, to display later:
public function runningTotal(){
global $db;
if($query = $db->prepare("SELECT amount FROM receipts WHERE user_id = ?")){
$query->bind_param("i", $this->uid);
$query->execute();
$query->bind_result($amount);
$running_total = 0;
while($query->fetch()){
$running_total += $amount;
}
$query->close();
}
return $running_total;
$db->close();
}
but i can’t figure out how to get the method periodReceipts() to behave similarly. I assume i need to get the data into an array, but how do i do that, and how do i access it later?
thanks!
Change your method like following
Then you’ll get all your records in array: