Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do custom query in magento?

Tags:

magento

I want to write a custom query in magento website.

I created a file test.php in my magento root folder & written a custom query

<?php
 $read= Mage::getSingleton('core/resource')->getConnection('core_read');
 $value=$read->query("Select * from catalog_product_flat_1");
 $row = $value->fetch();
 echo "<pre>";print_r($row);echo "</pre>";
?>

But it is not giving me any results.Please guide me.

like image 341
David Avatar asked Aug 03 '12 09:08

David


1 Answers

Try this:

$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql        = "Select * from catalog_product_flat_1";
$rows       = $connection->fetchAll($sql); //fetchRow($sql), fetchOne($sql),...
Zend_Debug::dump($rows);

In order to test, you can create sandbox.php file in root of your magento installation and paste the following code:

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql        = "Select * from catalog_product_flat_1";
$rows       = $connection->fetchAll($sql); //fetchRow($sql), fetchOne($sql),...
Zend_Debug::dump($rows);

and call from url as:

http://your-magento-url/sandbox.php
like image 74
MagePsycho Avatar answered Sep 19 '22 10:09

MagePsycho