B-219 Sec-55 Noida, India
+918010221733

Execute Custom Query In Magento

Magento provides very good feature for the data manipulations with the Database tables. Magento array give all the data by default but when you are working with the custom Magento development then we need to get or insert some data into the database. In this post I just wants to describes how it is possible to insert, delete or update data into the Magento by using the custom query.

Create Database Connections In Magento

<?php
getConnection(‘core_read’); 
//Only for reading the data from the database tables $write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
// Only for writing the data into the database tables 
?>  

Custom Insert Query in Magento

<?php
getConnection(‘core_write’);
 // insert 
$sql = “INSERT INTO `test_table` (`id`,`name`,`data`) 
VALUES (‘1′,’EWA’,’Just for the testing..’)”; $connection->query($sql);
?>  

Custom Select Query In Magento

<?php
getConnection(‘core_read’);
$value=$read->query(“select * from table”);
while ($row = $value->fetch()){ print_r($row); };
?> 

Custom Truncate a Table Query In Magento

<?php
getConnection(‘core_read’);
$read->query(“truncate table TableName”); 
?>  

Custom Update Query In Magento

<?php
 $data = array(“field” => “value”, “field” => “value”);
 $where = “id = 2”;
 $write->update(“TABLE_NAME”, $data, $where);
 $write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
 $write->query(“update TableName set field = ‘value'”);
?>

Custom Delete Query In Magento

<?php
 $where = “id = 2”;
 $write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
 $write->delete(“TABLE_NAME”, $where); //OR
 $write->query(“delete from TABLE_NAME where field = ‘value'”);
?>
(Visited 95 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.