2012-08-03 41 views
7

我想在magento网站上写一个自定义查询。如何在magento中进行自定义查询?

我以书面自定义查询

<?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>"; 
?> 

我的Magento的根文件夹&创建的文件test.php的,但它不给我任何results.Please指导我。

+1

您不应在Magento中编写任何自定义查询,请改用模型。 – OSdave 2012-08-03 11:06:45

回答

18

试试这个:

$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); 

为了测试,你可以在你的Magento安装根创建sandbox.php文件并粘贴以下代码:

<?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); 

和URL作为调用:

http://your-magento-url/sandbox.php 
+0

它没有显示任何东西。 – David 2012-08-03 10:42:55

+0

它应该工作。尝试将代码包装在try {} catch {}块中并打开开发人员模式。 – MagePsycho 2012-08-03 10:58:44

+0

非常感谢你MagePsycho! – David 2012-08-03 11:00:49