如何使用kohana从数据库中获取数据?

如何使用kohana从数据库中获取数据?

问题描述:

Hy!

I configured the modules/database/database.php file. In controller/index.php I have:

$query = DB::query(Database::SELECT, 'SELECT * FROM posts ORDER By id DESC');

Using phpmyadmin, I created two blog posts, but the script doesn't seem to get them from the database. I don't see any errors and also the blog posts are not visible.

P.S. Sorry for my bad English, I am a schoolboy from Latvia and I'm learning English. :)

Hy! p>

我配置了modules / database / database.php文件 。 在controller / index.php中我有: p>

  $ query = DB :: query(Database :: SELECT,'SELECT * FROM posts ORDER By id DESC'); 
   code>  pre> 
 
 

使用phpmyadmin,我创建了两篇博文,但该脚本似乎没有从数据库中获取它们。 我没有看到任何错误,也没有看到博客文章。 p>

P.S。 抱歉我的英语不好,我是来自拉脱维亚的小学生,我正在学习英语。 :) p> div>

please read documentation: "Once you are done building, you can execute the query using execute() and use the results."

$query = DB::query(Database::SELECT, 'SELECT * FROM posts ORDER By id DESC')->execute();

Now you can use foreach.

foreach($query as $item){  ..  }

I suggest to use Query Builder where it's possible to accidently avoid SQL injection in the future:

$query = DB::select()
            ->from('posts')
            ->order_by('id', 'DESC')
            ->execute();