Wednesday 4 June 2014

How to use select, update, delete and insert custom queries in magento

- We can Perform select, update, delete and insert queries through php file by following steps.


require_once '../../app/Mage.php';
Mage::app('default');


        Select query to get the value form table           

$connection = Mage::getSingleton('core/resource')
->getConnection('core_read');
$select = $connection->select()
->from('tablename', array('*')) // select * from tablename or use array('id','name') selected values
->where('id=?',1)               // where id =1
->group('name');         // group by name
$rowsArray = $connection->fetchAll($select); // return all rows
$rowArray =$connection->fetchRow($select);   //return row


             Insert query                  

$connection = Mage::getSingleton('core/resource')
->getConnection('core_write');
$connection->beginTransaction();
$fields = array();
$fields['name']= 'test';
$fields['age']='25';
$connection->insert('tablename', $fields);

$connection->commit();

            update query                 

$connection = Mage::getSingleton('core/resource')
->getConnection('core_write');
$connection->beginTransaction();
$fields = array();
$fields['name'] = 'jony';

//for single parameter
$where = $connection->quoteInto('id =?', '1');

/* For mulitple parameters
$where = $connection->quoteInto('id =?', '1')
                 .$connection->quoteInto('name =?', 'Mathi') ;
*/

$connection->update('tablename', $fields, $where);

$connection->commit();


                delete query                    

$condition = array($connection->quoteInto('id=?', '1'));

$connection->delete('tablename', $condition);




No comments:

Post a Comment