Magento中如何调用SQL语句

时间:2023-03-08 17:23:49
Magento中如何调用SQL语句

I. 创建表结构和测试数据

create table rooms(id int not null auto_increment, name varchar(100), primary key(id));
insert into rooms values(1,'Royal Room');
insert into rooms values(2,'Standard Room');

II.创建 controllers/RoomController.php:

  1. <?php
  2. class Cartz_Hotel_RoomController extends Mage_Core_Controller_Front_Action{
  3. public function listingAction() {
  4. $handle = Mage::getSingleton('core/resource')->getConnection('core_write');
  5. $query  = $handle->query('select name from rooms');
  6. while ($row = $query->fetch()) {
  7. $row = new Varien_Object($row);
  8. echo "<strong>" . $row->getName() . "</strong><br/>";
  9. }
  10. }
  11. }?>

在地址栏中输入: http://localhost/magento/index.php/hotel/room/listing, 页面将输出:

Royal Room

Standard Room