I am running
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT id FROM customers_old";
$old_customers = $connection->fetchAll($sql);
But it's a HUGH table and this does not fit into memory:
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted
Normally I would make a loop an fetch row by row: $connection->fetch($sql)
But fetch does not exist here and fetchRow() always returns the same row.
Solution?
- 
 If you are pulling that much data into PHP to process it then you are asking for trouble from the offset. Look for a better more memory efficient way to achieve the same result and pull only the data you need from the database. Also, importantly, you should be working with a collection in the situation above - using MySQL directly is a big no-no particularly as Magento is already a good way down the road to supporting multiple databases.Jonathan Hussey– Jonathan Hussey2014年08月05日 13:55:08 +00:00Commented Aug 5, 2014 at 13:55
3 Answers 3
You'll want to use the core/resource_iterator. This passes each object in a collection through a callback method. This article from Atwix (I'm not connected to them in any way) explains it really well: http://www.atwix.com/magento/working-with-large-collections/.
- 
 Note: this answer forgoes record set iteration and instead uses OO collection walking. Useful 99% of the time, but if you know what you're doing and actually want to use PDO-style iteration, see Renon Stewart's answer below.siliconrockstar– siliconrockstar2017年12月04日 05:07:00 +00:00Commented Dec 4, 2017 at 5:07
- 
 This is helpful, but you should always explain and excerpt the code in your answer while citing the source in case the link goes dead later.fantasticrice– fantasticrice2018年06月19日 19:25:41 +00:00Commented Jun 19, 2018 at 19:25
Take a look at How to convert sql query to magento in order to fetch data
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$readresult = $read->query("SELECT id FROM customers_old");
while ($row = $readresult->fetch() ) {
 $Ids[] = $row['id'];
}
See Conquer the 5 Most Common Magento Coding Issues to Optimize Your Site for Performance
- 
 REALLY don't recommend this approach.Jonathan Hussey– Jonathan Hussey2014年08月05日 14:09:26 +00:00Commented Aug 5, 2014 at 14:09
- 
 Good code! But DO NOT try to be smart and add it to 1 line like thiswhile ($row = $read->query($select)->fetch())as it will create an infinite loop, and this I have no idea why.Shadoweb– Shadoweb2017年10月31日 10:20:13 +00:00Commented Oct 31, 2017 at 10:20
- 
 @Shadowbob look at what fetch returns and you will understand.vitoriodachef– vitoriodachef2018年11月19日 09:03:58 +00:00Commented Nov 19, 2018 at 9:03
$tableName = 'customer_entity_varchar';
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$qry = "SELECT * FROM $tableName WHERE value = 240";
$result = $db->query($qry);
$data = $result->fetchAll();
echo '';print_r($data);
Try this, you can get this data in array format, if you wants to get single data then use $result->fetch()