How to use magento core query for read and write?

Most of the Magento developer preferred to use module model object or model resource object for Magento query, but even though some time need to use core query. To use core query method use below code.

/** Core resource object */
     $resource = Mage::getSingleton(‘core/resource’);
/** Read adapter connection */
    $read= $resource->getConnection(‘core_read’);
/** table name to fetch records */
     $clientsTable = $resource->getTableName(‘clients’);
/** Select query with where clause and order by */
     $select = $read->select()
     ->order(‘created_time DESC’) ;
/** Fetch records */
     $clients = $read->fetchRow($select);

/** Write adapter connection */
     $write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
     $sql = “insert into tablename values (?, ?, ?)”;
     $write->query($sql, array(‘aaa’,’bbb’,’ccc’));

Leave a Reply