Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to query YII2 database

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to query the YII2 database, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Database configuration.

/ config/db.php for database configuration

In the course of practice, there is a test library-"test table -" two records are as follows

Mysql > select * from test;+----+-+ | id | name | +-+-+ | 1 | zhuai | | 2 | heng | +-+-+ 18 rows in set (0.00 sec)

Sql query mode

Yii2 provides the original database query method findBySql; at the same time, through the placeholder way, automatically carries on the basic sql injection defense. Upper code

/ / the most basic query method: $sql = "select * from test where 1"; $res = Test::findBySql ($sql)-> all (); var_dump (count ($res)); / / res- > 2 / / findbysql prevent sql injection mode $id ='1 or 1 challenge challenge SQL = "select * from test where id =". $id;$res = Test::findBySql ($sql)-> all (); var_dump (count ($res)); / / res- > 2$ sql = "select * from test where id =: id"; / / Locator automatically prevents sql injection of $res = Test::findBySql (": id" = > $id)-> all (); var_dump (count ($res)); / / res- > 1

ActiveRecord query mode

In addition to the original sql mode, each framework will provide a corresponding encapsulated query mode, and so will yii2.

Create model

The basic way of model for yii is as follows, and the code is not detailed below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report