Magento中如何调用SQL语句

如何调用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:

    class Cartz_Hotel_RoomController extends Mage_Core_Controller_Front_Action{
       public function listingAction() {
          $handle = Mage::getSingleton('core/resource')->getConnection('core_write');

          $query  = $handle->query('select name from rooms');
          while ($row = $query->fetch()) {
             $row = new Varien_Object($row);
             echo "" . $row->getName() . "
"; } } }

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

    Royal Room
    Standard Room

相关文章:

1、Magento中获取Zend DB对象中的SQL语句
https://www.onexin.net/magento-zend-db-object-for-sql-statements/

2、Magento用SQL语句开发篇
https://www.onexin.net/posts-with-the-sql-statement-magento-development/

3、Magento常用SQL语句实用篇
https://www.onexin.net/practical-tips-commonly-used-sql-statement-magento/

4、教你看懂Magento中的SQL语句
https://www.onexin.net/magento-teach-you-to-understand-the-sql-statement/

5、创建 PDO 实例请求连接SQL数据库
https://www.onexin.net/pdo-database/

转载请注明出处:https://www.onexin.net/how-to-call-sql-statements-magento/

Leave a Reply

Your email address will not be published. Required fields are marked *