Skip to content

Instantly share code, notes, and snippets.

@rodde177
Forked from srinathweb/magento quering
Created February 6, 2017 12:03
Show Gist options
  • Save rodde177/295cba3af4c654b3c4a8d1ab23a198f5 to your computer and use it in GitHub Desktop.
Save rodde177/295cba3af4c654b3c4a8d1ab23a198f5 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