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 and process data in ThinkPHP5 framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to query and process data in ThinkPHP5 framework". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to query and process data in the ThinkPHP5 framework.

There are some problems in dealing with database query results. Record several query methods and result processing that have been used.

1. Query a record

$where=array ("version_id" = > $version_id); $data = model ("PackageWhitelist")-> where ($where)-> find (); $this- > assign ("package_id", $package_id); $where=array ("package_id" = > $package_id); $data = model ("Package")-> where ($where)-> find (); if ($data) {$this- > assign ("target_version", $data ['target_version']);}

two。 Query a record and a field

$device_number_list = model ("PackageWhitelist")-> where ($where)-> field ("device_number")-> find (); $this- > assign ("device_number", $device_number_list ['device_number'])

3. Query multiple records of a field and process the result, which is an array set

$where=array ("version_id" = > $version_id); $data = model ("PackageWhitelist")-> where ($where)-> field ("device_number")-> select (); $device_number_list='';foreach ($data as $val) {$list=$ val- > toArray (); if ($device_number_list) {$device_number_list=$device_number_list.';'.$list ["device_number"];} else {$device_number_list=$list ["device_number"];}}

4. Query multiple records

$where=array ("version_id" = > $version_id); $data = model ("PackageWhitelist")-> where ($where)-> select (); $device_number_list='';foreach ($data as $val) {$list=$ val- > toArray (); if ($device_number_list) {$device_number_list=$device_number_list.';'.$list ["device_number"];} else {$device_number_list=$list ["device_number"];}}

5. Query as a page and process the results.

Public function index ($version_id) {$where=array ("version_id" = > $version_id); $version_name = model ("Version")-> where ($where)-> field ("version_name")-> find (); $listrows=config ("LISTROWS")? config ("LISTROWS"): 10; $package_lists=model ("Package")-> where ($where)-> paginate ($listrows); $package_infos = $package_lists- > toArray () ["data"] Foreach ($package_infos as $key= > $value) {$package_infos [$key] = array ("source_version" = > $version_name ["version_name"]) + $package_infos [$key];}}

Let's summarize the three ways of querying databases in TP5.

Method 1: native sql query

Code example:

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

Development

Wechat

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

12
Report