magento how to select insert update and delete data

It is every important post because if we work on our custom module or extension than each time we need to communicate with our data base table.
If you want custom change on your Data base table in easy way than this post is helpful for you.

//For Select Query
$db_read = Mage::getSingleton('core/resource')->getConnection('core_read');
$tablePrefix = (string) Mage::getConfig()->getTablePrefix();

$sql = 'SELECT COLUMN_NAME FROM `' . $tablePrefix . 'TABLE_NAME`';

$data = $db_read->fetchAllRow($sql); // fetch All row in a table
$data = $db_read->fetchRow($sql); // fetch single row in a table
 print_r($data);  // to see the output of table


// For Insert Query.
 $db_write1 = Mage::getSingleton('core/resource')->getConnection('core_write');
        $tablePrefix = (string) Mage::getConfig()->getTablePrefix();

        $sql = 'INSERT INTO `' . $tablePrefix . 'TABLE_NAME`
VALUES ('COLUMN1_VALUE', 'COLUMN1_VALUE ')';
        $db_write1->query($sql);  


// For Delete Query
 $sql2 = 'DELETE FROM `' . $tablePrefix . 'TABLE_NAME` WHERE TABLE_COLUMN=COLUMN_VALUE';
 $db_write1->query($sql2);        
             

// For Update Query
 $updateQue = 'UPDATE `' . $tablePrefix . 'TABLE_NAME` SET TABLE_COLUMN1=COLUMN_VALUE1, TABLE_COLUMN2=COLUMN_VALUE2 WHERE TABLE_COLUMN=COLUMN_VALUE';
 $db_write1->query($updateQue);


Do you want custom data base connection in magento see here
              

1 comment: