Skip to content

Instantly share code, notes, and snippets.

@DanielGeek
DanielGeek / gist:8659d551c9ccf3618770a890b28ff7a9
Last active March 27, 2024 19:59
Rebilly Developer Coding Answers
Question 1
This issue is a result of a memory error generated when the code attempts to load the entire dataset from largeTable into memory using fetchAll(). This approach is not memory-efficient, particularly with large datasets, leading to the PHP error. To mitigate this, the code can be refactored to fetch and process data in chunks rather than all at once. Here's an improved approach using a loop and cursor:
In the refactored code snippet below, data is fetched row by row within a while loop using fetch() instead of fetchAll(). This means only one row of data is held in memory at any given time, significantly reducing memory usage during the process. It's important to ensure PDO::MYSQL_ATTR_USE_BUFFERED_QUERY is set to false before fetching the data to prevent PHP from buffering the entire result set in memory. This attribute is specific to MySQL; adjustments may be needed for other databases. After processing the data, remember to reset PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to its original state, especiall
#!/bin/bash
# Use this for your user data (script from top to bottom)
# install httpd (Linux 2 version)
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html