Directly query database

To directly send queries against the database:

//$read = Varien_Db_Adapter_Mysqli extends Zend_Db_Adapter_Mysqli extend Zend_Db_Adapter_Abstract
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
 
$query = "
    SELECT 
        SUM(`grand_total`) AS `grand_total`,
        SUM(`subtotal`) AS `subtotal`,
        SUM(`tax_amount`) AS `tax_amount`,
        SUM(`discount_amount`) AS `discount_amount`,
        SUM(`shipping_amount`) AS `shipping_amount`, 
    FROM `sns_salesreport_monthstats` 
    WHERE `year` = $year
    ";
$data = $read->fetchRow($query); //fetches only the first row

// or multiple rows:
$sql = “
    SELECT product_id  
    FROM catalog_category_product 
    WHERE category_id=3
    ″;
$data = Mage::getSingleton(‘core/resource’) ->getConnection(‘core_read’)->fetchAll($sql);

 

1 thoughts on “Directly query database

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.