The original post: /r/php by /u/NewYorker6135 on 2024-12-10 03:16:49.
I have a page that retrieves data from an MS SQL database so customers can log in to see their orders. For one customer who has a lot of orders the following message is appearing:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 8003 bytes)
This is the code:
$SQL="SELECT ordno, convert(char(10), orddate, 101) orddate, patient, convert(char(10), inv_date, 101) inv_date, convert(char(10), ship_date, 101) ship_date, tracking_no "; $SQL=$SQL . "FROM Orders WHERE ID_No='" . $_GET['id_no'] . "' ORDER BY Orddate DESC";
$qry = sqlsrv_query($conn, $SQL);
$result = array();
while ($row = sqlsrv_fetch_array($qry, SQLSRV_FETCH_ASSOC)) {
array_push($result, $row);
}
echo json_encode($result);
The actual data retrieved for this customer is about 7500 rows of 400 bytes each, for a total of about 3MB. So if the maximum is over 130MB, why is it being exhausted? Is there some kind of massive overhead involved that uses all that additional memory? I noticed that the message indicates it tried to allocate 8003 bytes, which is apparently the amount it allocates for each row. I don’t know why it’s allocating a memory block that’s so much larger than the actual size of the data. I am on a shared host so I don’t have the option of increasing the maximum allowable memory.