In order to run a SQL query against the Magento database, you first need a resource model then a database connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $db = Mage::getResourceSingleton('core/resource')->getConnection('core_write'); $result = $db->query('SELECT 'entity_id' FROM 'catalog_product_entity'); if (!$result) { return FALSE; } $rows = $result->fetch(PDO::FETCH_ASSOC); if (!$rows) { return FALSE; } print_r($rows); ?> |
Just so everyone else knows. To get rows of data do a while loop or similar.
here is my php test code…
this is error message…
Fatal error: Call to a member function getResourceModelInstance() on a non-object in /var/www/magento.homeunix.com/app/Mage.php on line 460
help me….please….
reposting…
my code..is………..
Fixed a couple problems in the above code:
1. I added a limit of 10 to the statement so it doesn’t crash your browser if you have a lot of products
2. Added “fetchAll” to the code so it fetches all the available rows (max 10 becuase of the above mentioned limit)
3. Added support for prefixes if your tables have a predefined prefix.
4. Changed to Mage::getSingleton(‘core/resource’). The above code is wrong.
5. Also, we should be using core_read here instead of core_write since we’re only reading data, but we’ll keep using core_write since it works for both.
Check out the Sweet Tooth reward points extension at http://www.getsweettooth.com which using this code somewhere in the source.
Cheers!
Thank you for sharing SQL query against the Magento database. Nice post. keep writing.
Thanks !!!
It has really helped me.
@Jay Thanks for your corrections
Nice ! works for me in app/design/frontend/default/default/template/page/2-colunms-left.phtml
saludos desde argentina