2012-08-03 28 views
7

Tôi muốn viết truy vấn tùy chỉnh trong trang web Magento.Cách thực hiện truy vấn tùy chỉnh trong Magento?

Tôi tạo ra một test.php tập tin trong thư mục gốc Magento tôi & viết một truy vấn tùy chỉnh

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

Nhưng nó không phải là cho tôi bất kỳ results.Please hướng dẫn cho tôi.

+1

bạn không nên viết bất kỳ truy vấn tùy chỉnh nào trong Magento, hãy sử dụng các mô hình thay thế. – OSdave

Trả lời

18

Hãy thử điều này:

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

Để kiểm tra, bạn có thể tạo tập tin sandbox.php trong thư mục gốc của cài đặt Magento của bạn và dán đoạn mã sau:

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

và gọi từ url như :

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

Nó không hiển thị bất cứ điều gì. – David

+0

Nó sẽ hoạt động. Cố gắng bọc mã của bạn trong khối try {} catch {} và bật chế độ nhà phát triển. – MagePsycho

+0

Cảm ơn bạn rất nhiều MagePsycho !! – David

Các vấn đề liên quan