Tuesday, 6 December 2011

Running custom query in Magento

For reading the data:

$read = Mage::getSingleton('core/resource')->getConnection('core_read');

// Prints a list of all website names

$query="SELECT * FROM core_website";  //select query as per requirement
$results = $read->fetchAll($query);
foreach($results as $row){
echo $row['name'] . "\n";
}

For read write(insert/update) both functionalty:

$write = Mage::getSingleton('core/resource')->getConnection('core_write');

$query="INSERT INTO cataloginventory_stock_item (`product_id`,`stock_id`,`qty`,`is_in_stock`) 
VALUES ( '.$_id.', 1, 99999, 1)";

$write->query( $query );

4 comments :

  1. Great post, I think people should acquire a lot from this blog its really user friendly. So much good information on here :D.

    ReplyDelete
  2. Outstanding post, I conceive website owners should acquire a lot from this site its rattling user friendly. So much excellent info on here :D.

    ReplyDelete
  3. I like this post, enjoyed this one thankyou for posting .

    ReplyDelete
  4. Simply a smiling visitant here to share the love (:, btw outstanding layout.

    ReplyDelete