Mgento_Bug_Fix

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

Using select, insert, delete and update the record in magento site is very easy.
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.

Using select, insert, delete and update the record in magento site is very easy.
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

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';
$where = $connection->quoteInto('id =?', '1');
$connection->update('tablename', $fields, $where);
$connection->commit();

delete query

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

Related posts:

  1. How to Fix Magento Login Problem in Chrome ?
  2. Magento install error – Exception printing is disabled by default for security reasons
  3. How To Fix Service Temporarily Unavailable in Magento