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!

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