magento insert and update and delet and all querys

It is very easy to select, insert, delete and update the record in
magento site. following functions are helpful in magento site for
database queries. You can also use this function outside magento
environment with the help pf include “Mage.php” file form “app” folder
like.
This is very simple to get data from database of magento site to frontend of magento site or another sites

  1. require_once ‘../../app/Mage.php’;
  2. Mage::app(‘default’);

Select query to get the value form table

  1. $connection = Mage::getSingleton(‘core/resource’)
  2. ->getConnection(‘core_read’);
  3. $select = $connection->select()
  4. ->from(‘tablename’, array(‘*’)) // select * from tablename or use array(‘id’,’name’) selected values
  5. ->where(‘id=?’,1)               // where id =1
  6. ->group(‘name’);         // group by name
  7. $rowsArray = $connection->fetchAll($select); // return all rows
  8. $rowArray =$connection->fetchRow($select);   //return row

insert query

  1. $connection = Mage::getSingleton(‘core/resource’)
  2. ->getConnection(‘core_write’);
  3. $connection->beginTransaction();
  4. $fields = array();
  5. $fields[‘name’]= ‘test’;
  6. $fields[‘age’]=’25’;
  7. $connection->insert(‘tablename’, $fields);
  8. $connection->commit();

update query

  1. $connection = Mage::getSingleton(‘core/resource’)
  2. ->getConnection(‘core_write’);
  3. $connection->beginTransaction();
  4. $fields = array();
  5. $fields[‘name’] = ‘jony’;
  6. $where = $connection->quoteInto(‘id =?’, ‘1’);
  7. $connection->update(‘tablename’, $fields, $where);
  8. $connection->commit();

delete query

  1. $condition = array($connection->quoteInto(‘id=?’, ‘1’));
  2. $connection->delete(‘tablename’, $condition);
Puneet Kumar Magento Developer
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply