12/23/2009

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(!$row) {
    return FALSE;
}

print_r($row);

Source : http://www.richardcastera.com/

No comments:

Post a Comment