Magento – How to run a SQL query against the database

In order to run a SQL query against the Magento database, you first need a resource model then a database connection.

$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);

Tags: ,

One Response to “Magento – How to run a SQL query against the database”

  1. Magento Designer

    Thank you for sharing SQL query against the Magento database. Nice post. keep writing.

Leave a Reply