dougudu3564 2015-09-23 13:39
浏览 45

Magento - 如何从MySQL表中获取数据并创建自定义选择菜单

I am using Magento 1.9.0.1 developing a custom extension. For that purpose i have to ask how can i run a custom MySQL query.

I want to run simple MySQL query with while loop. If it was a simple PHP script i am going to make it this way:

$r = mysql_query("SELECT * FROM `extensa_econt_city`");

    while($rowi = mysql_fetch_array($r))
            {
            $name = $rowi['name'];
            $city_id = addslashes($rowi['city_id']);
            echo "<option value='$city_id'>$name</option>";
            }

With this code in simple PHP i'll get all the rows and make them as options. I do not know however how can i get the information from table extensa_econt_city which is in the Magento database.

I will use this in a custom template file where i'll display that select menu. So guys can you please show me how can i run custom MySQL queries with while loop in Magento ?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douxi6903 2015-09-23 14:12
    关注

    Try like this

    $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
    $query      = "Select * from `extensa_econt_city`";
    $rows       = $connection->fetchAll($query);
    
    foreach ($rows as $values) {
        $name = $values['name'];
        $city_id = addslashes($values['city_id']);
        echo "<option value='$city_id'>$name</option>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用