dongti8535 2011-11-09 22:56
浏览 89
已采纳

Kohana中的数据库查询仅显示SQL而不显示结果行

I just started to using the Kohana framework and I'm trying to execute a few queries, but after spending a couple hours in documentation and running some tests all I have is this:

class Controller_Test extends Controller {
    public function action_index()
    {
       $query = DB::select()->from('test')->where('test', '=', '1');
       echo $query  
    }
}

Now, if try to run this all it does is echo my SQL.

How do I get the actual data from my database? I know that I could do something like:

$row = mysql_fetch_row(mysql_query($query));
echo $row[0];

and it would work; but I guess that's just completely stupid, since I'm using a framework and there has to be built-in method(s) for this.

  • 写回答

3条回答 默认 最新

  • doumei9589 2011-11-10 18:53
    关注

    After you've built up the query you'll then need to turn that into a Database_Query object using the execute() method so you can get values to run through.

    If you want to deal with the results of the query as an array, you can add the as_array() method after the execute like so:

    $query = DB::select()->from('test')->where('test', '=', '1')
                         ->execute()->as_array();
    

    If you're only dealing with a single row, and you know you only need a single row, you can change that to current():

    $query = DB::select()->from('test')->where('test', '=', '1')
                         ->execute()->current();
    

    Then you can extract as needed when you loop through the array:

    foreach ($query as $row)
    {
        echo $row['title'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面