开发者

How to get data from database with kohana?

开发者 https://www.devze.com 2023-03-31 19:34 出处:网络
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\');

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 f开发者_如何学运维rom Latvia and I'm learning English. :)


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消