Skip to content

Instantly share code, notes, and snippets.

@srinathweb
Created August 28, 2015 11:30
Show Gist options
  • Save srinathweb/c2fb6dcb9ef13b731540 to your computer and use it in GitHub Desktop.
Save srinathweb/c2fb6dcb9ef13b731540 to your computer and use it in GitHub Desktop.
magento quering
http://fishpig.co.uk/magento/tutorials/direct-sql-queries/
Yes you can run direct sql queries within Magento, the best way to do this is to use the read write resource. You can insatiate it with:
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$writeConnection = $resource->getConnection('core_write');
To run a select you can do something like this:
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
$results = $readConnection->fetchAll($query);
/* get the results */
var_dump($results);
To write something to the database use:
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$table = $resource->getTableName('catalog/product');
$query = "UPDATE {$table} SET {item} = '{value}' WHERE entity_id = 'value'";
$writeConnection->query($query);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment